我在使用python抓取网页时遇到了不同类型的问题.单击图像时,图像下会出现有关其“味道”的新信息.我的目标是解析连接到每个图像的所有风味.我的脚本可以解析当前活动图像的风格,但在单击新图像后会中断.我的循环中的一点点抽搐会引导我走向正确的方向.
我尝试过:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.optigura.com/uk/product/gold-standard-100-whey/")
wait = WebDriverWait(driver,10)
while True:
items = wait.until(EC.presence_of_element_located((By.XPATH,"//div[@class='colright']//ul[@class='opt2']//label")))
for item in items.find_elements_by_xpath("//div[@class='colright']//ul[@class='opt2']//label"):
print(item.text)
try:
links = driver.find_elements_by_xpath("//span[@class='img']/img")
for link in links:
link.click()
except:
break
driver.quit()
下面的图片可能会澄清我无法做到的事情:
最佳答案
我调整了代码以正确点击链接并检查当前列出的项目的文本是否与活动列出项目的文本匹配.如果它们匹配,您可以安全地继续解析而不必担心您一遍又一遍地解析相同的事情.干得好:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.optigura.com/uk/product/gold-standard-100-whey/")
wait = WebDriverWait(driver,10)
links = driver.find_elements_by_xpath("//span[@class='img']/img")
for idx,link in enumerate(links):
while True:
try:
link.click()
while driver.find_elements_by_xpath("//span[@class='size']")[idx].text != driver.find_elements_by_xpath("//div[@class='colright']//li[@class='active']//span")[1].text:
link.click()
print driver.find_elements_by_xpath("//span[@class='size']")[idx].text
items = wait.until(EC.presence_of_element_located((By.XPATH,"//div[@class='colright']//ul[@class='opt2']//label")))
for item in items.find_elements_by_xpath("//div[@class='colright']//ul[@class='opt2']//label"):
print(item.text)
except StaleElementReferenceException:
continue
break
driver.quit()