【发布时间】:2017-02-02 14:24:29
【问题描述】:
我正在抓取一个网站(https://www.zhihu.com/people/xie-ke-41/followers),我想获取所有关注者的信息。如您所见,一些关注者的信息是通过AJAX带来的,我使用chrome中的开发者工具并找到urlthe url which has followers' information
我的代码:
import requests
from bs4 import BeautifulSoup
zhihu_rl = 'https://www.zhihu.com/node/ProfileFollowersListV2'
data = {
'method': 'next',
'params': '{"offset":20,"order_by":"created","hash_id":"86858a7a4aa77d290364625efcaacb70"}'}
headers = {
'Host': 'www.zhihu.com',
'Origin': 'https://www.zhihu.com',
'Referer': 'https://www.zhihu.com/people/xie-ke-41/followers',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'X-Xsrftoken': 'foo',
'Cookie':'xxxxxxxxxxxx'}
rep = requests.post(url=zhihu_rl, data=data, headers=headers)
bsobj = BeautifulSoup(rep.text, 'html.parser')
print(bsobj.find_all('div', {'class': "zm-profile-card zm-profile-section-item zg-clear no-hovercard"}))
并返回一个空列表。 我可以看到信息是开发人员的工具: ,为什么bs4不能提取它们? PS:我可以得到所有的div,但是当我限制属性时,它失败了
【问题讨论】:
-
我无法访问该网站。从我所见,最后一行,项目之前不应该有任何空格吗?
zm-profile-section- item -
对不起,这是我的拼写错误
标签: python beautifulsoup web-crawler