第六课 Python新浪新闻爬虫最终整理总结
来源:互联网 发布:sql 日期 变量 慢 编辑:程序博客网 时间:2024/06/04 21:47
代码整理封装如图:
Python简洁又强大!
至此,教程圆满结束,还有什么不懂的或有疑问的问题,欢迎大家加我的QQ:1099718640
顺便再送上完整代码,凑凑字数,哈哈~(其实推荐大家去下载Github上的内容,顺便给个小心心什么的)
#第六课 封装整理#作者:DYBOY#时间:2017-09-06import requestsimport jsonimport refrom bs4 import BeautifulSoupfrom datetime import datetime#以上作为基本引用#获取评论数量def getCommentCounts(newsurl): newsid = re.search('doc-i(.*).shtml',newsurl).group(1) commentURL='http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-{}&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20' comments = requests.get(commentURL.format(newsid)) com_num = json.loads(comments.text.strip('var data='))['result']['count']['total'] return com_num#获取新闻详情页def getNewsDetail(newsurl): result = {} res = requests.get(newsurl) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') result['title'] = soup.select('#artibodyTitle')[0].text result['time'] = datetime.strptime(soup.select('.time-source')[0].contents[0].strip(),'%Y年%m月%d日%H:%M') result['source'] = soup.select('.time-source span a')[0].text result['article'] = ' '.join([p.text.strip() for p in soup.select('#artibody p')[:-1]]) result['editor'] = soup.select('.article-editor')[0].text.lstrip('责任编辑:') result['com_num'] = getCommentCounts(newsurl) return result#获取新闻列表def getNewsList(sortURL): res = requests.get(sortURL) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') for news in soup.select('.news-item'): if( len(news.select('h2')) >0 ): h2 = news.select('h2')[0].text time = news.select('.time')[0].text newsurl = news.select('a')[0]['href'] wenzhang = getNewsDetail(newsurl) print(time,h2,newsurl,wenzhang)#MAIN-获取新闻getNewsList('http://news.sina.com.cn/china/')
教程到这里就结束了,但是如果大家还有什么不懂的欢迎联系我,同时也希望各位能有所较大收获!
祝各位学业有成,事事顺心!
注:本文属于原创文章,转载请注明本文地址!
作者QQ:1099718640
CSDN博客主页:http://blog.csdn.net/dyboy2017
Github开源项目:https://github.com/dyboy2017/spider
阅读全文
1 0
- 第六课 Python新浪新闻爬虫最终整理总结
- Python 编写新浪新闻网络爬虫(学习整理)
- 第四课 Python爬虫简单爬取新浪新闻列表
- 第五课 Python爬虫抓取新浪新闻的内容页
- Python爬虫:抓取新浪新闻数据
- 第一个python爬虫 抓取新浪新闻
- python爬虫之获取新浪新闻信息
- python爬虫——获取新浪新闻前两页新闻信息
- python爬虫——新浪新闻网络爬虫
- 新浪新闻爬虫程序
- 使用python网络爬虫爬取新浪新闻(一)
- python获取新浪新闻
- python爬虫搜狐新闻
- python爬虫,爬取虎扑网新闻
- Python爬虫:新浪新闻详情页的数据抓取(函数版)
- Python爬虫学习(一)——爬取新浪新闻
- ptython3+mysql爬虫抓取新浪新闻
- Python_网络爬虫(新浪新闻抓取)
- Shell脚本学习
- 【06】vue.js — 生命周期
- 归并排序
- 使用fiddler更改post请求数据,具现post错误
- 使用ButterKnife方法,点击事件响应里面,执行performClick失效,不能再同一帧响应多个点击事件
- 第六课 Python新浪新闻爬虫最终整理总结
- 体验异步的终极解决方案-ES7的Async/Await
- 监听器,拦截器,过滤器的区别
- 驱动框架
- Ruby数组的各种使用
- java 浅析跨域问题以及如何使用Cors解决前后端分离部署项目所遇到的跨域问题
- .h 与 .hpp 文件
- 番茄助手Visual Assist X文中注释实用方法
- 《神经网络与深度学习》学习笔记