首页 文章

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

提问于
浏览
1

我正在使用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"  )

虽然汤和响应充满了html,但X是空的 . 我已经查看过了,我在inspect元素和requests.get(URL).content中看到的内容之间存在一些不一致,是怎么回事?

What Chrome inspect shows me

2 回答

  • 3

    那是因为它是使用Ajax / javascript加载的 . 请求库不处理它,你需要使用可以执行这些脚本并获取dom的东西 . 有各种选择,我会列出一对来帮助你入门 .

  • -1

    您应该尝试添加用户代理标头

    URL = 'http://allrecipes.com/cook/2010/reviews/'
    headers = {'user-agent', 'Mozilla/5.0'}
    response = requests.get(URL,headers=headers).content
    

相关问题