【问题标题】:Not return the answer and also say in title list index out of range不返回答案并在标题列表索引中说超出范围
【发布时间】:2021-02-21 13:06:57
【问题描述】:

不返回答案还说列表索引超出标题范围

url = 'https://www.adidas.com/us/women-athletic_sneakers'
driver = webdriver.Chrome('D:/chromedriver')
driver.get(url)
time.sleep(3)
time.sleep(2)

w_shoes = driver.find_elements_by_class_name('grid-item___3rAkS')
for shoe in w_shoes:
    
title = shoe.find_elements_by_xpath('//*[@id="product-search-results"]/div[2]/div[3]/div[4]/div[1]/div/div/div[3]/div[2]/a')[0].text
print(title)
    

【问题讨论】:

    标签: selenium-webdriver web-scraping python-requests webdriver selenium-grid


    【解决方案1】:

    您为什么不尝试使用可以在眨眼之间获取所需内容的请求?

    import requests
    
    url = "https://www.adidas.com/api/plp/content-engine?"
    
    params = {
        'sitePath': 'us',
        'query': 'women-athletic_sneakers'
    }
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
    }
    
    res = requests.get(url,params=params,headers=headers)
    for item in res.json()['raw']['itemList']['items']:
        print(item['displayName'])
    

    输出如下:

    Ultraboost OG Shoes
    Super Super Sleek 72 Shoes
    Forum Mid Shoes
    Forum Mid Shoes
    Ultraboost 21 Shoes
    

    【讨论】:

    • adidas.com/api/plp/content-engine?获取此网址的位置
    • ctrl + shift + i 打开开发工具,从那里选择network 选项卡,然后选择All 选项卡。现在重新加载页面并在左窗格中找到相关的 url。找到它后,只需单击该链接以获取更多详细信息。谢谢。
    • 我找到了,但他们没有价格,所以我该怎么办
    • 请尝试创建另一篇描述您当前问题的帖子。你问的问题已经回答了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2018-12-16
    • 2011-06-14
    相关资源
    最近更新 更多