【发布时间】:2021-10-13 07:22:51
【问题描述】:
我正在尝试抓取评论及其对 Snapdeal 的各种产品的星级评分。我正在通过产品 URL 访问产品。在特定页面上,我想根据星级过滤掉评级并获取评级编号以及评论。我正在使用以下代码来这样做
'''
url_snapdeal=('https://www.snapdeal.com/')
driver.get(url_snapdeal)
time.sleep(2)
search = driver.find_element_by_id('inputValEnter')
search.clear()
search.send_keys('smartphone')
search.send_keys(Keys.ENTER)
time.sleep(2)
for i in range(0,3):
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(1)
urls=[]
for link in driver.find_elements_by_xpath("//div[@class='product-desc-rating ']/a"):
urls.append(link.get_attribute('href'))
snap_reviews=[]
snap_ratings=[]
for url in urls:
driver.get(url)
time.sleep(4)
try:
for x in range(2,7):
driver.find_element_by_xpath("//div[@class='selectarea']").click()
time.sleep(1)
driver.find_element_by_xpath(f"//div[@class='options']/ul/li[{x}]").click()
time.sleep(1)
for rating in driver.find_elements_by_xpath("//div[@class='user-review']/div[1]"):
stars = rating.find_elements_by_xpath("i[@class='sd-icon sd-icon-star active']")
snap_ratings.append(len(stars))
except NoSuchElementException:
print('Not found')
try 块应该是点击星级过滤下拉菜单并选择 5star,收集星级评分和评论文本,再次点击下拉菜单,点击 4star 并收集评分和评论,等等。
我的代码设法点击下拉菜单,但无法点击 5 星、4 星等过滤选项。它会引发 ElementNotInteractable 异常。
任何帮助或建议将不胜感激。提前致谢。
【问题讨论】:
标签: python selenium web-scraping