详情页抓取的6个数据:新闻标题、评论数、时间、来源、正文、责任编辑。
首先,我们先将评论数整理成函数形式表示:
import requests
import json
import re
comments_url = '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'
def getCommentsCount(newsURL):
ID = re.search('doc-i(.+).shtml', newsURL)
newsID = ID.group(1)
commentsURL = requests.get(comments_url.format(newsID))
commentsTotal = json.loads(commentsURL.text.strip('var data='))
return commentsTotal['result']['count']['total']
news = 'http://news.sina.com.cn/c/nd/2017-05-14/doc-ifyfeius7904403.shtml'
print(getCommentsCount(news))
import requests
import json
import re
comments_url = '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'
def getCommentsCount(newsURL):
ID = re.search('doc-i(.+).shtml', newsURL)
newsID = ID.group(1)
commentsURL = requests.get(comments_url.format(newsID))
commentsTotal = json.loads(commentsURL.text.strip('var data='))
return commentsTotal['result']['count']['total']
news = 'http://news.sina.com.cn/c/nd/2017-05-14/doc-ifyfeius7904403.shtml'
print(getCommentsCount(news))
第5行comments_url,在上一篇中,我们知道评论链接中有新闻ID,不同新闻的评论数通过该新闻ID的变换而变换,因此我们将其格式化,新闻ID处用大括号{}来替代;
定义获取评论数的函数getCommentsCount,通过正则来查找匹配的新闻ID,然后将获取的新闻链接存储进变量commentsURL中,通过解码JS来得到最终的评论数commentsTotal;
然后,我们只需输入新的新闻链接,便可直接调用函数getCommentsCount来获取评论数。
最后,我们将需要抓取的6个数据均整理到一个函数getNewsDetail中。如下:
from bs4 import BeautifulSoup
import requests
from datetime import datetime
import json
import re
comments_url = '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'
def getCommentsCount(newsURL):
ID = re.search('doc-i(.+).shtml', newsURL)
newsID = ID.group(1)
commentsURL = requests.get(comments_url.format(newsID))
commentsTotal = json.loads(commentsURL.text.strip('var data='))
return commentsTotal['result']['count']['total']
# news = 'http://news.sina.com.cn/c/nd/2017-05-14/doc-ifyfeius7904403.shtml'
# print(getCommentsCount(news))
def getNewsDetail(news_url):
result = {}
web_data = requests.get(news_url)
web_data.encoding = 'utf-8'
soup = BeautifulSoup(web_data.text,'lxml')
result['title'] = soup.select('#artibodyTitle')[0].text
result['comments'] = getCommentsCount(news_url)
time = soup.select('.time-source')[0].contents[0].strip()
result['dt'] = datetime.strptime(time,'%Y年%m月%d日%H:%M')
result['source'] = soup.select('.time-source span 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('责任编辑:')
return result
print(getNewsDetail('http://news.sina.com.cn/c/nd/2017-05-14/doc-ifyfeius7904403.shtml'))
在函数getNewsDetail中,获取需要抓取的6个数据,放在result中:
- result['title']是获取新闻标题;
- resul['comments']是获取评论数,可以直接调用我们开头定义的评论数函数getCommentsCount;
- result['dt']是获取时间;
- result['source']是获取来源;
- result['article']是获取正文;
- result['editor']是获取责任编辑。
而后输入自己想要获取数据的新闻链接,调用该函数即可。
部分运行结果:
{'title': '浙大附中开课教咏春 “教头”系叶问第三代弟子', 'comments': 618, 'dt': datetime.datetime(2017, 5, 14, 7, 22), 'source': '中国新闻网', 'article': '原标题:浙大附中开课教咏春 “教头”系叶问......来源:钱江晚报', 'editor': '张迪 '}
Mac,Python版本:3.6,PyCharm版本:2016.2
网友评论