【问题标题】:Even the python code seems correct but attribute error occur, no text scraped甚至python代码似乎正确但发生属性错误,没有文字被刮掉
【发布时间】:2020-01-04 22:28:33
【问题描述】:

当我使用 BeautifulSoup 来抓取列出的产品名称和价格时,类似的代码在其他网站上也能正常工作。但是在这个网站上运行时,soup.findAll 属性都有,但是没有刮到文字,出现了AttributeError。有人可以帮忙看一下代码和网站检查吗?

我检查并运行了很多次,同样的问题仍然存在

代码在这里:

url = 'https://shopee.co.id/Handphone-Aksesoris-cat.40'
re = requests.get(url,headers=headers)
print(str(re.status_code))
soup = BeautifulSoup(re.text, "html.parser")
for el in soup.findAll('div', attrs={"class": "collection-card_collecton-title"}):
    name = el.get.text()
    print(name)

AttributeError: 'NoneType' 对象没有属性 'text'

【问题讨论】:

    标签: object web-scraping attributes attributeerror findall


    【解决方案1】:

    您在类名中缺少i。但是,内容从API调用动态加载(这就是为什么您无法在呼叫中找到它的原因,其中js未运行,因此下一个调用更新DOM不会发生);您可以在网络选项卡中找到。它返回JSON。

    import requests
    
    r = requests.get('https://shopee.co.id/api/v2/custom_collection/get?category_id=40&platform=0').json()
    titles = [i['collection_title'] for i in r['collections'][0]['list_popular_collection']]
    print(titles)
    


    价格也:

    import requests
    
    r = requests.get('https://shopee.co.id/api/v2/custom_collection/get?category_id=40&platform=0').json()
    titles,prices =zip(*[(i['collection_title'], i['price']) for i in r['collections'][0]['list_popular_collection']])
    print(titles,prices)
    

    【讨论】:

    • 非常感谢,是的,当我在这里发帖时,我确实很想念我。我运行的代码中有 i 。你说的对。我会试试你的代码,看看我能得到名字和同样的价格。再次感谢。
    • 谢谢!我试过并获得标题和价格。这是一个很棒的帮助,我在一天多。所以我是新的这个网站,我试过。我很幸运得到您如此迅速的回应和指导。
    • :downlock_url ='shopee.co.id/…'打印(download_url,sep ='\ n')在汤中的汤.findall('class',attrs = { “data-sqe”:“name”}):name1 = el.text name2 = name1.find(“class”)。文本打印(name2,',',ex汤中的print()打印()。 findAll('span', attrs={"class": "_341bF0"}): name2 = el.get_text() print(name2,',',end="") 如何将这些更改为您的 API 网址?
    • 我能够找到从网络选项卡获取。但是对于在我最后的评论中发布的巨大产品上市和价格,没有找到相关的Get - API站点信息。 span>
    • 谢谢。这真的好。 span>
    猜你喜欢
    • 2013-02-03
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多