我的for循环没有在我的gamestop刮刀上读到

2024-04-26 05:07:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能让他的for循环被阅读和采取的项目列表,它只是打印什么都没有,跳过整个循环 导入请求 进口re 从bs4导入 最大页数=10 关键字=“ps4” 成本最大值=0 成本最小值=0

def tradeSpiderGS(maxPages):
    page = 1
    while page <= maxPages:
        print(page)
        #creating url for soup
        if page <= 1:
        url = 'https://www.gamestop.com/browse?nav=16k-3-'+ keyword 
        +',28zu0'
        else:
            url = 'https://www.gamestop.com/browse?nav=16k-3-' + keyword + 
            ',2b'+ 
        str(page *12) + ',28zu0'
        #creating soup object
        srcCode = requests.get(url)
        plainTxt = srcCode.text
        soup = BeautifulSoup(plainTxt,"html.parser")

        #this for loop is not being read supposed to grab links on gs website
        for links in soup.find_all('a', {'class': 'ats-product-title-lnk'}):
            href = links.get('href')
            trueHref = 'https://www.gamestop.com/' + href
            print(trueHref)
        page += 1

tradeSpiderGS(maxPages)

Tags: httpscreatingcomurlforwwwpagelinks
1条回答
网友
1楼 · 发布于 2024-04-26 05:07:38

为什么循环不运行?

循环没有运行,因为soup.find_all('a', {'class': 'ats-product-title-lnk'})[](该类没有任何a)。你知道吗

该类没有任何a的原因是GameStop不允许您访问/browse页面,除非您先访问了正常页面。您可以通过以匿名模式在web浏览器中打开其中一个URL来确认这一点:

enter image description here

解决方法:

您可以使用不同的刮取机制(如python中的Selenium)来解决这个问题。您还可以将web浏览器请求中的头文件复制到request.get调用中,尽管我无法实现这一点。你知道吗

相关问题 更多 >