python3爬取新浪新闻内容

编程入门 行业动态 更新时间:2024-10-13 00:32:07

python3爬取新浪新闻<a href=https://www.elefans.com/category/jswz/34/1771374.html style=内容"/>

python3爬取新浪新闻内容

代码如下:

#commentsUrl用于获取新闻评论数等json信息
commentsUrl = '=1&format=js&channel=gn&newsid=comos-{}&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20&jsvar=loader_1487565938347_44362583'#获取评论数量的函数
def getCommentsCount(newsUrl):before = re.escape('doc-i')after = re.escape('.shtml')m = re.search(before + '(.+)' + after, newsUrl)newsId = m.group(1)comments = requests.get(commentsUrl.format(newsId))jd = json.loads(comments.text.strip('var loader_1487565938347_44362583='))commentCount = jd['result']['count']['total']return commentCount#获取新闻具体内容的函数
def getNewsDetail(newsUrl):result = {}res = requests.get(newsUrl)res.encoding = 'UTF-8'soup = BeautifulSoup(res.text,'html.parser')result['title'] = soup.select('#artibodyTitle')[0].textresult['newsSource'] = source = soup.select('#navtimeSource span a')[0].texttimesource = soup.select('#navtimeSource')[0].contents[0].strip()result['newsTime'] = datetime.strptime(timesource,'%Y年%m月%d日%H:%M')result['article'] = '\n'.join([p.text.strip() for p in soup.select('#artibody p')[:-1]])result['editor'] = soup.select('.article-editor')[0].text.strip('责任编辑:')result['commentsCount'] = getCommentsCount(newsUrl)return result#测试
result = getNewsDetail('.shtml')
print(result)

更多推荐

python3爬取新浪新闻内容

本文发布于:2024-02-19 13:19:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1763967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   新浪新闻

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!