【问题标题】:Python BeautifulSoup object returns nothing when find function is used使用 find 函数时 Python BeautifulSoup 对象不返回任何内容
【发布时间】:2020-03-25 12:46:55
【问题描述】:

我正在尝试从此website 获取内容;为此,我编写了以下代码:

source = requests.get(justwatch+pelicula,headers=headers)
source_content = source.content
soup2 = BeautifulSoup(source_content,'html.parser')
soup2.find_all('div', class_='price-comparison__grid__row__element')

但是我什么也没做,因为 find_all() 和 find()functions 返回 None。

我在这里错过了什么?

【问题讨论】:

  • 你看过“view-source:justwatch.com/es/pelicula/la-vida-es-bella”吗?您将看到页面实际上是动态生成的,因为大多数 HTML 内容不存在。因此,您将无法以这种方式获取内容,因为这样的请求不会执行页面上的脚本。您需要考虑使用 selenium 之类的工具,以便使用浏览器访问页面并执行脚本。

标签: python html beautifulsoup python-requests


【解决方案1】:

网站动态加载数据。从它的 api 和网站的 html 不包含带有class='price-comparison__grid__row__element' 的<div> 标签。这就是为什么你会得到None。试试python模块JustWatch

运行: pip install justwatch

【讨论】:

  • 谢谢,我不知道他们有 API
  • @sushiwithoutsushi 如果这回答了您的问题,请接受此作为答案以便问题结束
猜你喜欢
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 2020-01-26
  • 2018-10-02
  • 2022-11-28
  • 1970-01-01
相关资源
最近更新 更多