为什么我抓到的页面无法用BS正确提取数据啊?
我试图抓取一个人的知乎主页动态(代码如下),但采用这种方法抓取下来的页面里的</a></div>标签全部多了一个,变成了</a></div>,并且用BS模块提取数据,输出也非常奇怪,多了许多标签,以及根本不是我想要的TAG里的文本内容,请问如何解决这个问题?
# -*- coding:utf-8 –*- import sys import requests from bs4 import BeautifulSoup import re import requests reload(sys) sys.setdefaultencoding('utf8') # --------------------------------------- def test(): '''设置headers''' headers = { 'Connection': 'Keep-Alive', 'Accept': 'text/html, application/xhtml+xml, */*', 'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko', 'Accept-Encoding': 'gzip, deflate', 'Host': 'www.zhihu.com', 'DNT': '1' } s = requests.Session()#自动保存cookie,不用管它 '''登陆''' r = s.post('http://www.zhihu.com/login', data={'_xsrf': BeautifulSoup(s.get('http://www.zhihu.com/').content).find(type='hidden')['value'], 'email': 'email', 'password': 'password', 'rememberme': 'y'}, headers=headers) mo = s.post('http://www.zhihu.com/people/a-mo-mo-68/activities', data={'_xsrf': BeautifulSoup(s.get('http://www.zhihu.com/people/a-mo-mo-68').content).find_all( type='hidden')[1]['value'], 'start':'1433863506'} ) x = mo.content.decode("unicode-escape") soup = BeautifulSoup(x) content = soup.find('a',attrs={'class':'question_link'}) print content.get_text('n','</p>')
如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

发布评论
需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。