import pandas as pd
import requests
import json
from bs4 import BeautifulSoup
from datetime import datetime
import re
#当页跳转url
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={}'
url.encode('utf-8')
# #形成多页链接,并且解析每页的所有链接及每个链接的内容详情
#该篇文章评论url
commenturl = 'http://comment5.news.sina.com.cn/page/info?version=1&format=json&channel=sh&newsid=comos-{}&group=undefined&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=3'
##获取评论总数函数
def getcomments(newsurl):
m=re.search('doc-i(.*).shtml',newsurl)
newsid=m.group(1)
comments = requests.get(commenturl.format(newsid))
jd1 = json.loads(c
python3爬取新浪新闻文章内容代码
最新推荐文章于 2024-08-03 15:58:00 发布
本文展示了如何使用Python3进行网络爬虫,以获取新浪新闻的标题、时间、来源、评论数和内容。通过requests、BeautifulSoup等库解析JSON和HTML,实现对新浪新闻列表页的多页抓取,并将数据存储到CSV文件中。
摘要由CSDN通过智能技术生成