西安知名网站制作公司,网站建设开源,wordpress d9,大学校园网站建设方案爬虫#xff1a;红网网站#xff0c; 获取指定关键词与指定时间范围内的新闻#xff0c;并存储到CSV文件 V2.0#xff08;控制台版#xff09; 爬取目的#xff1a;为了获取某一地区更全面的在红网已发布的宣传新闻稿#xff0c;同时也让自己的工作更便捷 对比V1.0升级的… 爬虫红网网站 获取指定关键词与指定时间范围内的新闻并存储到CSV文件 V2.0控制台版 爬取目的为了获取某一地区更全面的在红网已发布的宣传新闻稿同时也让自己的工作更便捷 对比V1.0升级的内容可自定义输入查询的关键词、自定义获取的时间段内的新闻这样大家都可以用 环境Pycharm2021Python3.10 安装的包requestscsvbs4datetime 代码运行结果示例 爬虫完整代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time : 2024/4/4 21:36
# Author : LanXiaoFang
# Site :
# File : RedNet.py
# Software: PyCharm
import csv
import requests
from bs4 import BeautifulSoup
import datetimeheader {Accept: text/html,application/xhtmlxml,application/xml;q0.9,image/avif,image/webp,*/*;q0.8,Accept - Encoding: gzip, deflate, br,Accept - Language: zh-CN,zh;q0.8,zh-TW;q0.7,zh-HK;q0.5,en-US;q0.3,en;q0.2,Connection: keep - alive,Referer: https://news-search.rednet.cn/Search?q%E5%8F%8C%E7%89%8C,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0,Cookie: wdcid7486a2c50eaf8af8; Hm_lvt_c96b65e9975fa39afbd5e90222af5f391711378746,1711528844; Hm_lvt_aaecf8414f59c3fb0127932014cf53c71711378746,1711528844; __jsluid_s56e0acf3607072cce852b9d4fc556f54; Hm_lpvt_c96b65e9975fa39afbd5e90222af5f391711528844; Hm_lpvt_aaecf8414f59c3fb0127932014cf53c71711528844; __jsl_clearance_s1711530480.242|1|%2F%2BG2WNMEpLXiwlUgRr2hiMkP%2BMg%3D,Upgrade-Insecure-Requests: 1,
}article_Num_area 1 # 用于计在标题含指定区域的存储的表中的数据的序号
article_Num 1 # 用于计在标题不含但内容含指定区域的存储的表中的数据的序号
get_go 0 # 获取第几页开始的数据现在是0开始
count 0 # 用于计算总共爬取的新闻数量------Start Set 这一部分是自定义选项 查找自定义新闻------
# 爬取指定区域的文章 或者关键词 比如双牌 双牌县 优化营商环境······
print(爬取指定区域的文章 或者关键词 比如双牌 双牌县 优化营商环境)
# area 双牌
area input(请输入想要搜索的关键词)# 时间设定
# 想要获取的时间段 是个闭区间 年月日 xxxx-xx-xx
print(请输入你想要获取的时间段 是个闭区间 年月日 xxxx xx xx例如2024 3 4)
start_time input(请输入起始时间) # 起始时间包含起始日期这一天
start_time datetime.datetime.strptime(start_time, %Y %m %d)end_time input(请输入截止时间) # 截止时间包含截止日期这一天
end_time datetime.datetime.strptime(end_time, %Y %m %d)
------End Set 这一部分是自定义选项 查找自定义新闻------# 获取系统时间
now datetime.datetime.now()
year now.year # 年
month now.month # 月
day now.day # 日# # 创建CSV文件并写入头部信息
with open(str(month) 红网_标题含关键词.csv, w, newline, encodingutf-8) as csvfile:writer csv.writer(csvfile)writer.writerow([序号, 新闻名称, 新闻来源, 媒体级别, 发布日期, 原文链接]) # 根据实际情况定义列名
with open(str(month) 红网_标题不含内容含关键词.csv, w, newline, encodingutf-8) as csvfile:writer csv.writer(csvfile)writer.writerow([序号, 新闻名称, 信息来源, 媒体级别, 发布日期, 原文链接]) # 根据实际情况定义列名# 相当于满足条件就是一直循环
# while get_go 2: # 测试时用只获取前1-3页的新闻
while get_go 0:url https://news-search.rednet.cn/Search?q area s0o1r0p str(get_go)html_p requests.get(url, headersheader)html_p.encoding utf-8get_go 1if html_p.status_code 200:soups BeautifulSoup(html_p.text, html.parser)article_info soups.find_all(div, class_result)# print(len(article_info), \n)for i in article_info:result_info i.find_all(div, class_result-info)station_source result_info[0].select(span) # 选择result_info下的所有span标签station_info station_source[0].text # 文章发布站点source_info station_source[1].text # 文章来源print(station_info, source_info)# print(i.find_all(div, class_title), \n)title_info i.find_all(div, class_title)# 文章链接article_href title_info[0].a.get(href)print(article_href)# 升级版2.0这一部分注释掉了考虑通过文章链接进入文章详情页面获取 新闻来源 发布时间这样可以避免来源分析和计算时间的日期if station_info[3:] area 新闻网:# print(双牌新闻网文章链接, article_href, ---------, https://moment.rednet.cn/pc article_href[22:])article_href https://moment.rednet.cn/pc article_href[22:]# 修改文章来源为红网时刻if rednet in article_href:source_info 红网if moment.rednet in article_href:source_info 红网时刻if 来源 in source_info:source_info station_info[3:]# 文章标题article_title title_info[0].h3.text# 获取发布时间article_up_time title_info[0].span.textprint(发布时间, article_up_time)本来想直接进入文章详情页面直接获取时间的但是介于文章来源不同每种网站的时间所在标签也不一样由此还是决定在这里的时间信息进行处理了# 把显示为进入和昨天的时间改为具体的日期# 要注意 今天对应的昨天# ---如果是今天是1月1日则昨天的年月日应为上一年的12月31日要注意# ---如果今天是2-12月的1日则昨天的年月日应为上一月的最后一天if article_up_time 今天:article_up_time str(year) . str(month) . str(day)elif article_up_time 昨天:if day 1:if month 1:year - 1month 12day 31else:month - 1if month in [3, 5, 7, 8, 10, 12]:day 31elif month in [4, 6, 9, 11]:day 30elif month 2:if (year % 4 0 and year % 100 ! 0) or (year % 400 0): # 闰年2月day 29else:day 28article_up_time str(year) . str(month) . str(day - 1)# 修改时间显示格式-替换为.else:# article_up_time article_up_time[:4] . article_up_time[5:7] . article_up_time[8:10]article_up_time article_up_time.replace(-, .)# print(count, --名称, article_title, 来源, source_info, 日期, article_up_time, 链接, article_href)# 得到这篇文章发布的时间的日期格式date_article_up_time datetime.datetime.strptime(article_up_time, %Y.%m.%d)# 现在有个问题怎么退出循环时间不满足就退出现在获取到的新闻的时间开始时间就退出if date_article_up_time start_time:get_go -1break# 把满足自定义时间的新闻内容保存到csv表格中if start_time date_article_up_time end_time:count 1# date_article_up_time datetime.datetime.strftime(date_article_up_time, %Y.%m.%d)print( count, 名称, article_title, 来源, source_info, 日期, date_article_up_time, 链接, article_href)# 把数据存入表格 根据标题或内容 是否含有 #{area} 关键词 分开存储if area in article_title:# 这个是标题含有#{area}的with open(str(month) 红网_标题含关键词.csv, a, newline, encodingutf-8) as csvfile:writer csv.writer(csvfile)writer.writerow([article_Num_area, article_title, source_info, 级, article_up_time, article_href])article_Num_area 1else:# 这个是标题不含但是内容含有#{area}的with open(str(month) 红网_标题不含内容含关键词.csv, a, newline,encodingutf-8) as csvfile:writer csv.writer(csvfile)writer.writerow([article_Num, article_title, source_info, 级, article_up_time, article_href])article_Num 1 文章转载自: http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.klwxh.cn.gov.cn.klwxh.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.ysbhj.cn.gov.cn.ysbhj.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn