【发布时间】:2018-10-30 16:04:31
【问题描述】:
我有一个使用 Python/Selenium 的代码块,它应该遍历网页上的各种项目并显示该项目是否可用,如果可用,则显示该项目的名称和颜色并提供页面的链接。该代码在售罄的商品上按预期工作,但是当它到达第一个可用商品时,python 只返回“可用”并提供页面上最后一个商品的名称/url,而不是预期的商品。代码块:
shirts = driver.find_elements_by_xpath("""//*[@id='container']/article/div/h1/a""")
colors = driver.find_elements_by_xpath("""//*[@id='container']/article/div/p/a""")
articles = driver.find_elements_by_tag_name('article')
for article in articles:
ActionChains(driver).move_to_element(article).perform()
if article.find_element_by_tag_name('a').text == "sold out":
print("sold out")
elif article.find_element_by_tag_name('a').text == "":
ActionChains(driver).move_to_element(article).perform()
print("available")
for shirt, color in zip(shirts, colors):
shirt_text = shirt.text
color_text = color.text
print shirt_text, color_text
link = article.find_element_by_xpath('div/a').get_attribute('href')
print(link)
这是上面代码返回的 sn-p: (应显示的预期项目是 Plaza Sunglasses Magenta)
sold out
sold out
available
Supreme®/Hanes® Crew Socks (4 Pack) White
https://www.supremenewyork.com/shop/accessories/zf83g0dx4/hijz30rq8
sold out
sold out
sold out
sold out
sold out
sold out
以及我正在抓取的页面链接: http://www.supremenewyork.com/shop/all/accessories
我的脚本设置不正确还是我完全遗漏了什么?
【问题讨论】:
标签: python list selenium web-scraping zip