Reputation: 69
I am trying to scrape amazon reviews for a certain product, but I am unable to locate the text for the ratings using selenium. But the same thing is easily scraped using soup.
Here is my code using Soup:
link='same link as mentioned above'
url=requests.get(link).content
bs=soup(url,'html.parser')
for i in bs.find_all('span',{'class':'a-icon-alt'}):
print(i.text.split(' ')[0])
##Output 4.3 5.0 1.0 5.0 2.0 4.0 1.0 5.0 5.0 5.0 5.0 5.0 5.0
Here is my code using Selenium:
import time
from selenium import webdriver
from bs4 import BeautifulSoup as soup
import requests
link='link to the above mentioned page'
driver=webdriver.Chrome()
driver.get(link)
for i in driver.find_elements_by_css_selector('.a-icon-alt'):
print(i.text)
I am unable to get the same results with Selenium, all I get are blanks equivalent to the number of items present on that page. I have also tried using XPath and class_name but didn't get the required response.
Upvotes: 1
Views: 44
Reputation: 33384
To get the review ratings Induce WebDriverWait
and wait for presence_of_all_elements_located
() and use get_attribute("innerHTML")
instead of text
Code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
link='https://www.amazon.in/BenQ-inch-Bezel-Monitor-Built/product-reviews/B073NTCT4R/ref=cm_cr_arp_d_paging_btm_next_2?ie=UTF8&reviewerType=all_reviews&pageNumber=39'
driver=webdriver.Chrome()
driver.get(link)
elements=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".a-icon-alt")))
for i in elements:
print(i.get_attribute("innerHTML").split(' ')[0])
Output on console :
4.3
5.0
1.0
5.0
2.0
4.0
1.0
5.0
5.0
5.0
5.0
5.0
5.0
Upvotes: 1