我正在尝试从中获取每个产品的名称和价格 https://www.daraz.pk/catalog/?q=risk但什么也没有出现。
containers = page_soup.find_all("div",{"class":"c2p6A5"})
for container in containers:
pname = container.findAll("div", {"class": "c29Vt5"})
name = pname[0].text
price1 = container.findAll("span", {"class": "c29VZV"})
price = price1[0].text
print(name)
print(price)
最佳答案
页面中有JSON数据,可以在<script>
中获取使用 beautifulsoup 标记,但我认为这不是必需的,因为您可以直接使用 json
获取它和 re
import requests, json, re
html = requests.get('https://.......').text
jsonStr = re.search(r'window.pageData=(.*?)</script>', html).group(1)
jsonObject = json.loads(jsonStr)
for item in jsonObject['mods']['listItems']:
print(item['name'])
print(item['price'])
关于python - 在父 div beautifulsoup 中打印文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53792144/