1. 起因, 目的:
- 继续做新闻爬虫。我之前写过。
- 此文先记录2个新闻来源。
- 后面打算进行过滤,比如只选出某一个类型新闻。
2. 先看效果
过滤出某种类型的新闻,然后生成 html 页面,而且,自动打开这个页面。
比如科技犯罪类的新闻。
3. 过程:
代码 1 ,爬取东方网
- 很久之前写过,代码还能用。
- 这里虽然是复制一下,也是为了自己方便。
python
import os
import csv
import time
import requests
"""
# home: https://sh.eastday.com/
# 1. 标题, url, 来源,时间
"""
headers = {
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36'
}
def get_data(pages):
file_name = '5.8.400.csv' # 400个标题。
has_file = os.path.exists(file_name)
# 打开文件,写入模式
with open(file_name, 'a', newline='', encoding='utf-8') as file:
# 创建一个csv.DictWriter对象,用于写入字典数据
columns = ['title', 'url', 'time','source']
writer = csv.DictWriter(file, fieldnames=columns)
# 写入表头
if not has_file:
writer.writeheader()
# 爬取数据. 默认是 20页,每页20条。 每天大概有400个新闻。
for i in range(pages):
print(f"正在爬取第{i+1} / {pages}页数据")
time.sleep(0.5)
url = f"https://apin.eastday.com/apiplus/special/specialnewslistbyurl?specialUrl=1632798465040016&skipCount={i * 20}&limitCount=20"
resp = requests.get(url, headers=headers)
if resp.status_code!= 200:
print(f"请求失败:{resp.status_code}")
break
ret = resp.json()
junk = ret['data']['list']
for x in junk:
item = dict()
# print(x)
item["time"] = x["time"]
item['title'] = x["title"]
item["url"] = x["url"]
item["source"] = x["infoSource"]
# 写入数据
writer.writerow(item)
# print(item)
get_data(pages=20)
代码 2 , 爬取, 澎湃新闻
- 也是很简单。
python
import os
import csv
import time
import requests
from datetime import datetime, timedelta
# 请求头
headers = {
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36',
'Content-Type': 'application/json', # 响应头要求 Content-Type
'Referer': 'https://www.thepaper.cn/', # 引荐来源,遵循 strict-origin-when-cross-origin
'Origin': 'https://www.thepaper.cn' # 跨域请求需要 Origin
}
def get_thepaper_data(file_name='peng_pai_400.csv', max_pages=100, channel_id='-8'):
"""
爬取澎湃新闻数据,保存到 CSV 文件
参数:
file_name: 输出 CSV 文件名
max_pages: 最大爬取页数
channel_id: 新闻频道 ID
"""
# 检查文件是否存在
has_file = os.path.exists(file_name)
# 打开 CSV 文件,追加模式
with open(file_name, 'a', newline='', encoding='utf-8') as file:
columns = ['title', 'url', 'time', 'source']
writer = csv.DictWriter(file, fieldnames=columns)
if not has_file:
writer.writeheader()
# 计算 startTime(当前时间戳)
current_time = int(time.time() * 1000) # 当前毫秒时间戳
start_time = current_time # 使用此时此刻的时间
# 爬取数据
for page in range(1, max_pages + 1):
time.sleep(0.5) # 请求间隔
payload = {
'channelId': channel_id,
'excludeContIds': [], # 留空,需根据实际需求调整
'province': '',
'pageSize': 20,
'startTime': start_time,
'pageNum': page
}
url = 'https://api.thepaper.cn/contentapi/nodeCont/getByChannelId'
resp = requests.post(url, headers=headers, json=payload, timeout=10)
if resp.status_code != 200:
print(f"请求失败:{url}, 状态码: {resp.status_code}, 页码: {page}")
break
ret = resp.json()
# print(f"页面 {page} 响应:{ret}")
news_list = ret['data']['list']
for item in news_list:
# print(item)
news = {}
news['title'] = item.get('name', '')
news['url'] = f"https://www.thepaper.cn/newsDetail_forward_{item.get('originalContId', '')}"
news['time'] = item.get('pubTimeLong', '')
news['source'] = item.get('authorInfo', {}).get('sname', '澎湃新闻')
# 转换时间格式(如果 API 返回时间戳)
news['time'] = datetime.fromtimestamp(news['time'] / 1000).strftime('%Y-%m-%d %H:%M:%S')
# 直接写入,不去重
writer.writerow(news)
print(f"保存新闻:{news}")
if __name__ == "__main__":
get_thepaper_data(file_name='peng_pai_400.csv', max_pages=20, channel_id='-8')
4. 结论 + todo
1 数据来源,还需要增加。可选项:
- 上观新闻 shobserver.com 与解放日报关联,报道上海本地案件。
- 新浪新闻 news.sina.com.cn 全国性新闻,包含科技犯罪。
- 腾讯新闻 news.qq.com 聚合多种来源,覆盖广泛。
- 聚合。 提取出自己感兴趣的新闻,比如,科技犯罪。
希望对大家有帮助。