2016-04-26 79 views
1

我正在使用BeautifulSoup和Requests刮取allrecipes用户数据。Requests.content与Chrome检测元素不匹配

当检查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

回答

3

这是因为它使用Ajax/JavaScript的加载。请求库不处理,你需要使用一些可以执行这些脚本并获得dom的东西。有各种选项,我会列出一对夫妇,让你开始。

+1

感谢您的回答,硒工作了巨大的。在将来如何识别网站正在加载Ajax/JavaScript? –

-1

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

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