python爬虫——获取新浪新闻前两页新闻信息
来源:互联网 发布:软装搭配软件 编辑:程序博客网 时间:2024/06/05 16:03
import requestsimport jsonfrom bs4 import BeautifulSoupimport reimport jsonfrom datetime import datetimecommentURL='http://comment5.news.sina.com.cn/page/info?version=1&\ format=js&channel=sh&newsid=comos-{}&\ group=&compress=0&ie=utf-8&oe=utf-8&page=1&\ page_size=20'def getCommentCount(newsurl): m=re.search('doc-i(.+).shtml',newsurl) newsid=m.group(1) comments=requests.get(commentURL.format(newsid)) jd=json.loads(comments.text.strip('var data=')) return jd['result']['count']['total']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['newssource']=soup.select('.time-source span a')[0].text timesource=soup.select('.time-source')[0].contents[0].strip() result['dt']=datetime.strptime(timesource,'%Y年%m月%d日%H:%M') result['article']=' '.join([p.text.strip() for p in soup.select('#artibody p')[:-1]]) result['editor']=soup.select('.show_author')[0].text.lstrip('责任编辑:') result['comments']=getCommentCount(newsurl) return result#news='http://news.sina.com.cn/c/nd/2017-12-09/doc-ifypnyqi2478315.shtml'#print(getNewsDetail(news))def parserlistlinks(url): newsdetails=[] res=requests.get(url) jd=json.loads(res.text.lstrip(' newsloadercallback(').rstrip(');')) for ent in jd['result']['data']: newsdetails.append(getNewsDetail(ent['url'])) return newsdetails#news='http://api.roll.news.sina.com.cn/zt_list?channel=news&cat_1=gnxw&cat_2==gdxw1||=gatxw||=zs-pl||=mtjj&level==1||=2&show_ext=1&show_all=1&show_num=22&tag=1&format=json&page=2&callback=newsloadercallback&_=1512876098022'#print(parserlistlinks(news))url='http://api.roll.news.sina.com.cn/zt_list?channel=news&cat_1=gnxw&cat_2==gdxw1||=gatxw||=zs-pl||=mtjj&level==1||=2&show_ext=1&show_all=1&show_num=22&tag=1&format=json&page={}&callback=newsloadercallback&_=1512876098022'news_total=[]for i in range(1,3): newsurl=url.format(i) newsary=parserlistlinks(newsurl) news_total.extend(newsary) print(news_total)#print(len(news_total))'''import pandasdf=pandas.DataFrame(news_total)#print(df.head(2))print(df.to_excel('news.xlsx'))'''
阅读全文
0 0
- python爬虫——获取新浪新闻前两页新闻信息
- python爬虫之获取新浪新闻信息
- 获取新浪新闻信息
- python爬虫——新浪新闻网络爬虫
- python获取新浪新闻
- Python爬虫:抓取新浪新闻数据
- 第一个python爬虫 抓取新浪新闻
- 爬虫学习——爬虫之新浪新闻
- 新浪新闻爬虫程序
- Python爬虫学习(一)——爬取新浪新闻
- 第四课 Python爬虫简单爬取新浪新闻列表
- 第五课 Python爬虫抓取新浪新闻的内容页
- 第六课 Python新浪新闻爬虫最终整理总结
- Python 编写新浪新闻网络爬虫(学习整理)
- 使用python网络爬虫爬取新浪新闻(一)
- python爬虫搜狐新闻
- python爬虫,爬取虎扑网新闻
- pyhton爬虫(8)——获取网易新闻内容
- 算法设计思想入门
- 【操作系统】IO 缓冲技术
- JAVA程序员怎么样才能进一线互联网公司
- Docker架构
- 浅谈Object Head First Java笔记
- python爬虫——获取新浪新闻前两页新闻信息
- 登录注册搜索(封装mvp)
- sql笔记
- Android_RecyclerView
- Java读取TXT文件的方法
- std 中for_each 算法与c++11范围for语句
- Redis 分布式锁的正确实现方式( Java 版 )
- springmvc 处理的过程详解
- SSH信任关系设置-oracle rac 信任关系