好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

新浪新闻详情页的数据抓取实例

上一篇文章《Python爬虫:抓取新浪新闻数据》详细解说了如何抓取新浪新闻详情页的相关数据,但代码的构建不利于后续扩展,每次抓取新的详情页时都需要重新写一遍,因此,我们需要将其整理成函数,方便直接调用。

详情页抓取的6个数据:新闻标题、评论数、时间、来源、正文、责任编辑。

首先,我们先将评论数整理成函数形式表示:

 1 import requests 2 import json 3 import re 4  5 comments_url = '{}&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20' 6  7 def getCommentsCount(newsURL): 8     ID = re.search('doc-i(.+).shtml', newsURL) 9     newsID = ID.group(1)10     commentsURL = requests.get(comments_url.format(newsID))11     commentsTotal = json.loads(commentsURL.text.strip('var data='))12     return commentsTotal['result']['count']['total']13 14 news = ''15 print(getCommentsCount(news)) 

查看更多关于新浪新闻详情页的数据抓取实例的详细内容...

  阅读:39次