feat: 添加中国环境报
This commit is contained in:
parent
fe8ff3400f
commit
299cb8765a
185
国内党媒/CrawlZhongguohuanjingbao.py
Normal file
185
国内党媒/CrawlZhongguohuanjingbao.py
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
# _*_ coding : UTF-8 _*_
|
||||||
|
# @Time : 2024/11/24 04:06
|
||||||
|
# @UpdateTime : 2024/11/24 04:06
|
||||||
|
# @Author : haochen zhong
|
||||||
|
# @File : CrawlZhongguohuanjingbao.py
|
||||||
|
# @Software : PyCharm
|
||||||
|
# @Comment : 本程序采集中国环境报数据
|
||||||
|
import asyncio
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup, Comment
|
||||||
|
from httpx import AsyncClient
|
||||||
|
from motor.motor_asyncio import AsyncIOMotorClient
|
||||||
|
|
||||||
|
start_date = datetime.strptime('2013-08', '%Y-%m')
|
||||||
|
"""中国环境报2013年8月份开始有数据"""
|
||||||
|
end_date = datetime.today()
|
||||||
|
"""截止到今天"""
|
||||||
|
headers = {
|
||||||
|
'User-Agent': 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42'}
|
||||||
|
|
||||||
|
pattern = r'<a href=([^>]+)><div[^>]*>([^<]+)</div></a>'
|
||||||
|
# 链接数据库
|
||||||
|
client = AsyncIOMotorClient('mongodb://localhost:27017')
|
||||||
|
db = client['buweijiguanbao']
|
||||||
|
collection = db['zhongguohuanjingbao']
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
collection_names = await db.list_collection_names()
|
||||||
|
# 判断数据表是否存在
|
||||||
|
if "zhongguohuanjingbao" not in collection_names:
|
||||||
|
print("中国环境报数据表不存在,开始采集!")
|
||||||
|
await getData(start_date, end_date)
|
||||||
|
else:
|
||||||
|
# 如果存在,则从数据库中获取最后一条记录的日期
|
||||||
|
last_record = await collection.find_one({}, sort=[('release_time', -1)])
|
||||||
|
last_date_str = last_record['release_time']
|
||||||
|
print("数据库截止时间:", last_date_str)
|
||||||
|
await getData(last_date_str, end_date)
|
||||||
|
|
||||||
|
|
||||||
|
async def getContent(soup: BeautifulSoup) -> str:
|
||||||
|
"""
|
||||||
|
:param soup: BeautifulSoup对象
|
||||||
|
:return: 文章内容
|
||||||
|
"""
|
||||||
|
content = ""
|
||||||
|
for p in soup.select("#ozoom p"):
|
||||||
|
para = p.text.strip()
|
||||||
|
if para:
|
||||||
|
content += para
|
||||||
|
content += '\n'
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
async def getData(start_date: datetime, end_date: datetime):
|
||||||
|
"""
|
||||||
|
:param start_date: 开始日期
|
||||||
|
:param end_date: 结束日期
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
crawl_num = 0
|
||||||
|
# 创建一个列表保存月份
|
||||||
|
months = []
|
||||||
|
# 从开始日期到结束日期,每个月份都添加到列表中
|
||||||
|
current_date = start_date
|
||||||
|
while current_date <= end_date:
|
||||||
|
months.append(current_date)
|
||||||
|
# 增加一个月
|
||||||
|
if current_date.month == 12:
|
||||||
|
current_date = current_date.replace(year=current_date.year + 1, month=1)
|
||||||
|
else:
|
||||||
|
current_date = current_date.replace(month=current_date.month + 1)
|
||||||
|
# 遍历月份列表
|
||||||
|
for month in months:
|
||||||
|
# 构造URL
|
||||||
|
url = f'http://news.cenews.com.cn/html/{month.strftime("%Y-%m")}/period.xml'
|
||||||
|
"""http://news.cenews.com.cn/html/2013-08/period.xml"""
|
||||||
|
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url)
|
||||||
|
async with AsyncClient(headers=headers, timeout=60) as client:
|
||||||
|
response = await client.get(url)
|
||||||
|
response.encoding = response.charset_encoding
|
||||||
|
print(f"一级连接状态:{response.status_code}")
|
||||||
|
if response.status_code == 200:
|
||||||
|
soup = BeautifulSoup(response.text, 'xml')
|
||||||
|
for period in soup.select("period"):
|
||||||
|
period_name = datetime.strptime(period.find("period_name").text.strip(), "%Y-%m-%d")
|
||||||
|
front_page = period.find("front_page").text.strip()
|
||||||
|
try:
|
||||||
|
url1 = f"http://news.cenews.com.cn/html/{period_name.strftime('%Y-%m/%d')}/{front_page}"
|
||||||
|
"""http://news.cenews.com.cn/html/2013-08/14/node_2.htm"""
|
||||||
|
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url1)
|
||||||
|
response2 = await client.get(url1)
|
||||||
|
response2.encoding = response2.charset_encoding
|
||||||
|
print(f"二级连接状态:{response2.status_code}")
|
||||||
|
if response2.status_code == 200:
|
||||||
|
soup2 = BeautifulSoup(response2.text, 'lxml')
|
||||||
|
for item in soup2.select("#pgn #pageLink"):
|
||||||
|
banmianming = item.text.split(": ")[-1]
|
||||||
|
banmianhao = item.text.split(": ")[0]
|
||||||
|
url2 = f"http://news.cenews.com.cn/html/{period_name.strftime('%Y-%m/%d')}/" + item.get(
|
||||||
|
"href").replace("./", "").strip()
|
||||||
|
"""http://news.cenews.com.cn/html/2013-08/14/node_2.htm"""
|
||||||
|
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url2)
|
||||||
|
response3 = await client.get(url2)
|
||||||
|
response3.encoding = response3.charset_encoding
|
||||||
|
print(f"三级连接状态:{response3.status_code}")
|
||||||
|
if response3.status_code == 200:
|
||||||
|
for href, title in re.findall(pattern, response3.text, re.IGNORECASE):
|
||||||
|
url3 = f"http://news.cenews.com.cn/html/{period_name.strftime('%Y-%m/%d')}/" + href.replace(
|
||||||
|
"./", "").strip()
|
||||||
|
"""http://news.cenews.com.cn/html/2013-08/14/content_17724.htm"""
|
||||||
|
if await collection.find_one({"detail_url": url3}, {"_id": False}):
|
||||||
|
continue
|
||||||
|
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url3)
|
||||||
|
response4 = await client.get(url3)
|
||||||
|
response4.encoding = response4.charset_encoding
|
||||||
|
print(f"四级连接状态:{response4.status_code}")
|
||||||
|
if response4.status_code == 200:
|
||||||
|
soup4 = BeautifulSoup(response4.text, 'html.parser')
|
||||||
|
try:
|
||||||
|
comments = soup4.find_all(string=lambda text: isinstance(text, Comment))
|
||||||
|
for comment in comments:
|
||||||
|
if 'enpproperty' in comment:
|
||||||
|
# 提取注释内容
|
||||||
|
enpproperty_content = comment.strip()
|
||||||
|
inner_soup = BeautifulSoup(enpproperty_content, 'html.parser')
|
||||||
|
# 提取特定标签值
|
||||||
|
title = inner_soup.find('founder-title').text
|
||||||
|
perTitle = inner_soup.find('founder-introtitle').text
|
||||||
|
subTitle = inner_soup.find("founder-subtitle").text
|
||||||
|
author = inner_soup.find('founder-author').text
|
||||||
|
keywordlist = inner_soup.find("founder-keyword").text
|
||||||
|
except:
|
||||||
|
title = title
|
||||||
|
perTitle = ""
|
||||||
|
subTitle = ""
|
||||||
|
author = ""
|
||||||
|
keywordlist = ""
|
||||||
|
content = await getContent(soup4)
|
||||||
|
await collection.insert_one({
|
||||||
|
"title": title,
|
||||||
|
"subtitle": subTitle,
|
||||||
|
"preTitle": perTitle,
|
||||||
|
"author": author,
|
||||||
|
"banmianming": banmianming,
|
||||||
|
"banmianhao": banmianhao,
|
||||||
|
'keywordlist': keywordlist,
|
||||||
|
'detail_url': url3,
|
||||||
|
'release_time': period_name,
|
||||||
|
'insert_timestamp': datetime.today(),
|
||||||
|
'content': content
|
||||||
|
})
|
||||||
|
crawl_num += 1
|
||||||
|
print(
|
||||||
|
f"中国环境报---{period_name.strftime('%Y-%m-%d')}----{banmianming}---{banmianhao}---{title}---采集完成!")
|
||||||
|
await asyncio.sleep(random.randint(5, 15))
|
||||||
|
print(
|
||||||
|
f"中国环境报---{period_name.strftime('%Y-%m-%d')}----{banmianming}---{banmianhao}-----采集完成!")
|
||||||
|
await asyncio.sleep(random.randint(5, 15))
|
||||||
|
print(
|
||||||
|
f"中国环境报---{period_name.strftime('%Y-%m-%d')}------采集完成!")
|
||||||
|
await asyncio.sleep(random.randint(5, 15))
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
await collection.insert_one(
|
||||||
|
{'banmianhao': 'empty',
|
||||||
|
'banmianming': 'empty',
|
||||||
|
'preTitle': 'empty',
|
||||||
|
'title': 'empty',
|
||||||
|
'subtitle': 'empty',
|
||||||
|
'author': 'empty',
|
||||||
|
'keywordlist': 'empty',
|
||||||
|
'detail_url': url,
|
||||||
|
'release_time': period_name,
|
||||||
|
'insert_timestamp': datetime.today(),
|
||||||
|
'content': 'empty'}
|
||||||
|
)
|
||||||
|
print(f"中国环境报采集完毕,共采集{crawl_num}条数据!")
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
27
国内党媒/todo.md
27
国内党媒/todo.md
@ -1,13 +1,14 @@
|
|||||||
| 序号 | 完成状态 | 部委 | 部委报纸名称 | 网址 | 数据库 | 数据表 | 备注 |
|
| 序号 | 完成状态 | 部委 | 部委报纸名称 | 网址 | 数据库 | 数据表 | 备注 |
|
||||||
|:------:|:----:|:-----------------:|:-------:|:--------------------------------------------------------------------------------------------------------------:|:--------------:|:----------------------:|:----------------------:|
|
|:--:|:----:|:-----------------:|:-------------------------------------------:|:--------------------------------------------------------------------------------------------------------------:|:--------------:|:----------------------:|:----------------------:|
|
||||||
| 1 | ✅ | 中华人民共和国国家发展和改革委员会 | 中国改革报 | [www.cfgw.net.cn](http://www.cfgw.net.cn/epaper/201709/05/node_01.htm) | buweijiguanbao | zhongguogaigebao | 中国改革报2017年9月份开始有数据 |
|
| 1 | ✅ | 中华人民共和国国家发展和改革委员会 | [中国改革报](./CrawlZhongguogaigebao.py) | [www.cfgw.net.cn](http://www.cfgw.net.cn/epaper/201709/05/node_01.htm) | buweijiguanbao | zhongguogaigebao | 中国改革报2017年9月份开始有数据 |
|
||||||
| 2 | ✅ | | 中国经济导报 | [www.ceh.com.cn](http://www.ceh.com.cn/epaper/uniflows/html/2024/11/07/01/default.htm) | buweijiguanbao | zhongguojingjidaobao | 中国经济导报2012年9月份开始有数据 |
|
| 2 | ✅ | | [中国经济导报](./CrawlZhongguojingjidaobao.py) | [www.ceh.com.cn](http://www.ceh.com.cn/epaper/uniflows/html/2024/11/07/01/default.htm) | buweijiguanbao | zhongguojingjidaobao | 中国经济导报2012年9月份开始有数据 |
|
||||||
| 3 | ✅ | 中华人民共和国教育部 | 中国教育报 | [paper.jyb.cn](http://paper.jyb.cn/zgjyb/html/2024-11/23/node_1.htm) | buweijiguanbao | zhongguojiaoyubao | 中国教育报2022年1月份开始有数据 |
|
| 3 | ✅ | 中华人民共和国教育部 | [中国教育报](./CrawlZhongguojiaoyubao.py) | [paper.jyb.cn](http://paper.jyb.cn/zgjyb/html/2024-11/23/node_1.htm) | buweijiguanbao | zhongguojiaoyubao | 中国教育报2022年1月份开始有数据 |
|
||||||
| 4 | ✅ | 中华人民共和国科学技术部 | 科技日报 | [digitalpaper.stdaily.com](https://digitalpaper.stdaily.com/http_www.kjrb.com/kjrb/html/2024-11/22/node_2.htm) | buweijiguanbao | kejiribao | 科技日报2011年1月份开始有数据 |
|
| 4 | ✅ | 中华人民共和国科学技术部 | [科技日报](./CrawlKejiribao.py) | [digitalpaper.stdaily.com](https://digitalpaper.stdaily.com/http_www.kjrb.com/kjrb/html/2024-11/22/node_2.htm) | buweijiguanbao | kejiribao | 科技日报2011年1月份开始有数据 |
|
||||||
| 5 | ✅ | 中华人民共和国工业和信息化部 | 人民邮电报 | [rmydb.cnii.com.cn](https://rmydb.cnii.com.cn/html/2024/20241030/20241030_001/20241030_001_6709.html#0) | buweijiguanbao | renminyoudianbao | 人民邮电报2024年9月份开始有数据 |
|
| 5 | ✅ | 中华人民共和国工业和信息化部 | [人民邮电报](./CrawlRenminyoudianbao.py) | [rmydb.cnii.com.cn](https://rmydb.cnii.com.cn/html/2024/20241030/20241030_001/20241030_001_6709.html#0) | buweijiguanbao | renminyoudianbao | 人民邮电报2024年9月份开始有数据 |
|
||||||
| 6 | ✅ | 中华人民共和国国家民族事务委员会 | 中国民族报 | [210.12.104.26:81](http://210.12.104.26:81/epaper/) | buweijiguanbao | zhongguominzubao | 中国民族报2022年1月份开始有数据 |
|
| 6 | ✅ | 中华人民共和国国家民族事务委员会 | [中国民族报](./CrawlZhongguominzubao.py) | [210.12.104.26:81](http://210.12.104.26:81/epaper/) | buweijiguanbao | zhongguominzubao | 中国民族报2022年1月份开始有数据 |
|
||||||
| 7 | ✅ | 中华人民共和国公安部 | 人民公安报 | [epaper.cpd.com.cn](http://epaper.cpd.com.cn/szb/wwwcpd_9/dzb_16465/rmga/2020/2020_04_05/) | buweijiguanbao | renmingonganbao | 人民公安报2020年4月1日开始有数据 |
|
| 7 | ✅ | 中华人民共和国公安部 | [人民公安报](./CrawlRenmingonganbao.py) | [epaper.cpd.com.cn](http://epaper.cpd.com.cn/szb/wwwcpd_9/dzb_16465/rmga/2020/2020_04_05/) | buweijiguanbao | renmingonganbao | 人民公安报2020年4月1日开始有数据 |
|
||||||
| 8 | ✅ | 中华人民共和国民政部 | 中国社会报 | [epaper.shehuiwang.cn](https://epaper.shehuiwang.cn/site/index.jsp) | buweijiguanbao | zhongguoshehuibao | 中国社会报2022年12月01日开始有数据 |
|
| 8 | ✅ | 中华人民共和国民政部 | [中国社会报](./CrawlZhongguoshehuibao.py) | [epaper.shehuiwang.cn](https://epaper.shehuiwang.cn/site/index.jsp) | buweijiguanbao | zhongguoshehuibao | 中国社会报2022年12月01日开始有数据 |
|
||||||
| 9 | ✅ | 中华人民共和国司法部 | 法治日报 | [epaper.legaldaily.com.cn](http://epaper.legaldaily.com.cn/fzrb/content/20210101/Page01TB.htm) | buweijiguanbao | fazhiribao | 法治日报2021年1月1日开始有数据 |
|
| 9 | ✅ | 中华人民共和国司法部 | [法治日报](./CrawlFazhiribao.py) | [epaper.legaldaily.com.cn](http://epaper.legaldaily.com.cn/fzrb/content/20210101/Page01TB.htm) | buweijiguanbao | fazhiribao | 法治日报2021年1月1日开始有数据 |
|
||||||
| 10 | ✅ | 中华人民共和国财政部 | 中国财经报 | [114.118.9.73](http://114.118.9.73/epaper/) | buweijiguanbao | zhongguocaijingbao | 中国财经报2017年11月份开始有数据 |
|
| 10 | ✅ | 中华人民共和国财政部 | [中国财经报](./CrawlZhongguocaijingbao.py) | [114.118.9.73](http://114.118.9.73/epaper/) | buweijiguanbao | zhongguocaijingbao | 中国财经报2017年11月份开始有数据 |
|
||||||
| 11 | ✅ | 中华人民共和国自然资源部 | 中国自然资源报 | [szb.iziran.net](http://szb.iziran.net/bz/html/index.html?date=2024-11-21&cid=1) | buweijiguanbao | zhongguoziranziyuanbao | 中国自然资源报2018年5月18日开始有数据 |
|
| 11 | ✅ | 中华人民共和国自然资源部 | [中国自然资源报](./CrawlZhongguoziranziyuanbao.py) | [szb.iziran.net](http://szb.iziran.net/bz/html/index.html?date=2024-11-21&cid=1) | buweijiguanbao | zhongguoziranziyuanbao | 中国自然资源报2018年5月18日开始有数据 |
|
||||||
|
| 12 | ✅ | 中华人民共和国生态环境部 | [中国环境报](./CrawlZhongguohuanjingbao.py) | [news.cenews.com.cn](http://news.cenews.com.cn/html/2024-10/30/node_2.htm) | buweijiguanbao | zhongguohuanjingbao | 中国环境报2013年8月开始有数据 |
|
||||||
Loading…
x
Reference in New Issue
Block a user