python - 在父 div beautifulsoup 中打印文本

标签 python web-scraping beautifulsoup

我正在尝试从中获取每个产品的名称和价格 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/

相关文章:

python - Beautiful Soup and Table Scraping - lxml 与 html 解析器

python - BeautifulSoup - 如何从网站提取电子邮件?

python - 在python中使用beautifulsoup获取链接名称和 "selecting"链接而不是限制?

python - 使用 Python 抓取 Javascript 创建的动态内容

python - 如何将其从Python转换为C?

python - Ipython3 和 Ipython2 冲突

Python unittest - 如何将调试信息添加到 TestResult 对象?

python - 使用 Python 对 Sicstus 的操作系统系统调用无限期挂起

python - 无法使用 BeautifulSoup 抓取嵌套 html

python - 使用 BeautifulSoup 抓取 IMDb 页面