即使 python 代码似乎是正确的,但发生属性错误,没有文本被抓取

Even the python code seems correct but attribute error occur, no text scraped

当我使用 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)

A​​ttributeError: 'NoneType' 对象没有属性 'text'

您在 class 名称中缺少 i。但是,内容是从 API 调用动态加载的(这就是为什么在 js 没有 运行 的调用中找不到它的原因,因此下一次更新 DOM 调用没有'不会发生);您可以在网络选项卡中找到。它returns 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)