Requests.content 与 Chrome 检查元素不匹配

2024-01-02

我正在使用 BeautifulSoup 和 Requests 来抓取所有食谱用户数据。

当检查 HTML 代码时,我发现我想要的数据包含在

<article class="profile-review-card">

但是当我使用以下代码时

URL = 'http://allrecipes.com/cook/2010/reviews/'
response = requests.get(URL ).content
soup = BeautifulSoup(response, 'html.parser')
X = soup.find_all('article', class_ = "profile-review-card"  )

虽然 soup 和 response 充满了 html,但 X 是空的。我查看了一下,发现检查元素和 requests.get(URL).content 之间存在一些不一致,这是怎么回事?

Chrome 检查显示的内容 https://i.stack.imgur.com/5VtgG.png


那是因为它是使用 Ajax/javascript 加载的。 Requests 库无法处理这个问题,您需要使用可以执行这些脚本并获取 dom 的东西。有多种选择,我将列出几个以帮助您入门。

  • Selenium http://selenium-python.readthedocs.org/
  • ghost.py http://jeanphix.me/Ghost.py/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Requests.content 与 Chrome 检查元素不匹配 的相关文章

随机推荐