Selenium无法爬取Vivino品酒笔记等信息?求助解决方案
问题:Selenium爬取Vivino品酒笔记和食物搭配时出现NoSuchElementException
我尝试获取Vivino平台上API无法访问的品酒笔记和食物搭配信息,但使用Python的Selenium时一直出现NoSuchElementException。目前能正常爬取价格和年份信息,但无法获取页面下方的相关数据。
目标页面:https://www.vivino.com/US-TX/en/villa-maria-auckland-private-bin-sauvignon-blanc/w/39034?year=2021&price_id=26743464
已尝试的方法
- 使用WebDriverWait等待页面加载:
driver.get('https://www.vivino.com/US-TX/en/villa-maria-auckland-private-bin-sauvignon-blanc/w/39034?year=2021&price_id=26743464') WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@data-testid='mentions']")))
- 使用XPath获取风味关键词:
tasting_notes = driver.find_elements(By.XPATH, "//div[@data-testid='mentions']")
- 通过类名获取文本(已注释):
#test = driver.find_elements(By.CLASS_NAME,"tasteNote__flavorGroup--1Uaen")
- 滚动至页面底部后爬取:
while True: # Scroll down to the bottom. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Wait to load the page. time.sleep(2) # Calculate new scroll height and compare with last scroll height. new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height
以上方法均触发NoSuchElementException,想知道是否有其他方法可获取该信息,或是Vivino限制了此部分的爬取?
更新
问题已解决!感谢furas的解释和Eugeny提供的代码。
内容的提问来源于stack exchange,提问作者olive47
相关产品推荐
相关产品推荐

