python爬虫beta版之抓取知乎单页面回答(low 逼版)
来源:互联网 发布:wto贸易统计数据库 编辑:程序博客网 时间:2024/06/03 14:49
闲着无聊,逛知乎。发现想找点有意思的回答也不容易,就想说要不写个爬虫帮我把点赞数最多的给我搞下来方便阅读,也许还能做做数据分析(意淫中~~)
鉴于之前用python写爬虫,帮运营人员抓取过京东的商品品牌以及分类,这次也是用python来搞简单的抓取单页面版,后期再补充哈。
#-*- coding: UTF-8 -*- import requestsimport sysfrom bs4 import BeautifulSoup#------知乎答案收集----------#获取网页body里的内容def get_content(url , data = None): header={ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235' } req = requests.get(url, headers=header) req.encoding = 'utf-8' bs = BeautifulSoup(req.text, "html.parser") # 创建BeautifulSoup对象 body = bs.body # 获取body部分 return body#获取问题标题def get_title(html_text): data = html_text.find('span', {'class': 'zm-editable-content'}) return data.string.encode('utf-8')#获取问题内容def get_question_content(html_text): data = html_text.find('div', {'class': 'zm-editable-content'}) if data.string is None: out = ''; for datastring in data.strings: out = out + datastring.encode('utf-8') print '内容:\n' + out else: print '内容:\n' + data.string.encode('utf-8')#获取点赞数def get_answer_agree(body): agree = body.find('span',{'class': 'count'}) print '点赞数:' + agree.string.encode('utf-8') + '\n'#获取答案def get_response(html_text): response = html_text.find_all('div', {'class': 'zh-summary summary clearfix'}) for index in range(len(response)): #获取标签 answerhref = response[index].find('a', {'class': 'toggle-expand'}) if not(answerhref['href'].startswith('javascript')): url = 'http://www.zhihu.com/' + answerhref['href'] print url body = get_content(url) get_answer_agree(body) answer = body.find('div', {'class': 'zm-editable-content clearfix'}) if answer.string is None: out = ''; for datastring in answer.strings: out = out + '\n' + datastring.encode('utf-8') print out else: print answer.string.encode('utf-8')html_text = get_content('https://www.zhihu.com/question/43879769')title = get_title(html_text)print "标题:\n" + title + '\n'questiondata = get_question_content(html_text)print '\n'data = get_response(html_text)
输出结果:
0 0
- python爬虫beta版之抓取知乎单页面回答(low 逼版)
- python爬虫学习之抓取一个页面
- python 爬虫抓取页面图片
- python爬虫之urllib2登录并抓取HTML页面
- 利用python爬虫抓取OJ上做题信息(终结版)
- 利用python爬虫抓取OJ上做题信息(扩展版)
- python爬虫:抓取页面上的超链接
- Python爬虫实战之抓取淘宝MM照片(一)
- Python爬虫实战之抓取淘宝MM照片(二)
- Python爬虫实战之抓取淘宝MM照片(三)
- Python爬虫实战之抓取淘宝MM照片(四)
- python爬虫实践之网页抓取
- Python爬虫之抓取网络图片
- Python爬虫之知乎图片抓取
- Python实现抓取页面上链接的简单爬虫分享
- Python实现抓取页面上链接的简单爬虫分
- PyThon 抓取页面(入门)
- python爬虫抓取全国pm2.5的空气质量(2015.12.21版)
- Spark SQL技术介绍
- mybatis-Mybatis Generator工具的使用和配置
- C#——MVC从服务端解决JS跨域请求问题
- 编辑pdf文件时的一些问题如何解决
- tf-idf weighting
- python爬虫beta版之抓取知乎单页面回答(low 逼版)
- Ubuntu中安装Docker
- 帆软激活码
- 关于虚拟存储器、虚拟内存和交换空间的理解
- 浅谈 qmake 之 shadow build
- Log日志简陋封装
- 在树莓派上搭建ad-hoc网络教程
- 普通报表的删除功能
- Dynamics 365 APP -- 清晰定义你的系统职责范围