卡在 for 循环中的第一个元素上

时间:2020-12-28 03:02:27

标签: python selenium selenium-webdriver web-scraping

我已经编写了一个脚本,可以在 Steam 市场上进行刮擦。我正在使用 for 循环来检查不同的列表,但我只使用第一个元素有问题,所以我只能得到第一个列表。

这是我正在使用的代码:

driver = uc.Chrome()
driver.get('https://chrome.google.com/webstore/detail/csgofloat-market-checker/jjicbefpemnphinccgikpdaagjebbnhg/related?hl=en')
time.sleep(4)
driver.get('https://steamcommunity.com/market/listings/730/%E2%98%85%20Gut%20Knife%20%7C%20Case%20Hardened%20%28Factory%20New%29')
time.sleep(3)

articles = driver.find_elements_by_xpath('//span[starts-with(@id, "listing_")]')
time.sleep(3)
for article in articles:
    print(article.text)
    pris = article.find_element_by_xpath('//*[starts-with(@class, "market_listing_price market_listing_price_with_fee")]' and '//span[contains(@class, "market_listing_price market_listing_price_with_fee")]')
    Float = article.find_element_by_xpath('//*[starts-with(@class, "csgofloat-itemseed")]')
    print(pris.text)
    print(Float.text)

当我运行它时,输出是:

★ Gut Knife  | Case Hardened (Fabriksny)
747,50zł
Paint Seed: 981
★ Gut Knife  | Case Hardened (Fabriksny)
747,50zł
Paint Seed: 981
★ Gut Knife  | Case Hardened (Fabriksny)
747,50zł
Paint Seed: 981

1 个答案:

答案 0 :(得分:0)

基本上 xpath '//*[starts-with(@class, "market_listing_price market_listing_price_with_fee")]' and '//span[contains(@class, "market_listing_price market_listing_price_with_fee")]' 在语法上是错误的,正确的是 '//*[starts-with(@class, "market_listing_price market_listing_price_with_fee") and contains(@class, "market_listing_price market_listing_price_with_fee")]' 或更简单的 '//*[starts-with(@class, "market_listing_price market_listing_price_with_fee")]'。此外,我无法在页面中找到路径为 '//*[starts-with(@class, "csgofloat-itemseed")]' 的元素。

除此之外,当您遍历元素时,必须以点开头路径才能访问给定元素的子元素,我认为这是您的问题。所以你可以这样做:

articles_box = driver.find_elements_by_xpath("//div[starts-with(@class, 'market_listing_row market_recent_listing_row')]")
for a in articles_box:
    article = a.find_element_by_xpath('.//span[starts-with(@id, "listing_")]')
    print(article.text)
    pris = a.find_element_by_xpath('.//*[starts-with(@class, "market_listing_price market_listing_price_with_fee")]')
    Float = a.find_element_by_xpath('.//*[starts-with(@class, "csgofloat-itemseed")]')
    print(pris.text)
    print(Float.text)

(我真的建议你使用 WebDriverWait 而不是 time sleeps)