From b86a7737cbd6da6efd346dcaae5ff3709a86172f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=93=E6=9C=88=E5=BD=92=E5=B0=98?= Date: Mon, 11 Nov 2024 20:23:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=AD=E5=9B=BD?= =?UTF-8?q?=E7=BB=8F=E6=B5=8E=E5=AF=BC=E6=8A=A5=E5=81=B6=E5=B0=94=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E6=97=A5=E6=9C=9F=E5=8C=B9=E9=85=8D=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 国内党媒/CrawlZhongguojingjidaobao.py | 68 +++++++++++++-------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/国内党媒/CrawlZhongguojingjidaobao.py b/国内党媒/CrawlZhongguojingjidaobao.py index 39c9f2e..37a86bf 100644 --- a/国内党媒/CrawlZhongguojingjidaobao.py +++ b/国内党媒/CrawlZhongguojingjidaobao.py @@ -66,6 +66,7 @@ async def getData(start_date: datetime, end_date: datetime): months = [] # 从开始日期到结束日期,每个月份都添加到列表中 current_date = start_date + current_date = current_date.replace(day=1) while current_date <= end_date: months.append(current_date) # 增加一个月 @@ -78,23 +79,23 @@ async def getData(start_date: datetime, end_date: datetime): # 构造URL url = f'http://www.ceh.com.cn/epaper/uniflows/html/{month.strftime("%Y/%m")}/date.txt' """http://www.ceh.com.cn/epaper/uniflows/html/2012/09/date.txt""" - print(url) - async with AsyncClient(headers=headers, timeout=60) as client: - # 发送GET请求 - response = await client.get(url) - response.encoding = "gb2312" - print(f"一级连接状态:{response.status_code}") - if response.status_code == 200: - # 解析XML - soup = response.text.split("|") - for period in soup: - period_id, element = period.split(",") - if len(element) < 5: - continue - try: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url) + try: + async with AsyncClient(headers=headers, timeout=60) as client: + # 发送GET请求 + response = await client.get(url) + response.encoding = "gb2312" + print(f"一级连接状态:{response.status_code}") + if response.status_code == 200: + # 解析XML + soup = response.text.split("|") + for period in soup: + period_id, element = period.split(",") + if len(element) < 5: + continue url1 = f"http://www.ceh.com.cn/epaper/uniflows/html/{month.strftime('%Y/%m')}/{period_id}/boardurl.htm" """http://www.ceh.com.cn/epaper/uniflows/html/2012/09/01/boardurl.htm""" - print(url1) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url1) response2 = await client.get(url1) response2.encoding = "gb2312" print(f"二级连接状态:{response2.status_code}") @@ -106,7 +107,7 @@ async def getData(start_date: datetime, end_date: datetime): """http://www.ceh.com.cn/epaper/uniflows/html/2024/11/07/01/default.htm""" banmianming = item.text.split(":")[-1].strip() banmianhao = item.text.split(":")[0].replace(" ", "").replace(" ", "").strip() - print(url2) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url2) response3 = await client.get(url2) response3.encoding = "gb2312" print(f"三级连接状态:{response3.status_code}") @@ -118,7 +119,7 @@ async def getData(start_date: datetime, end_date: datetime): if await collection.find_one({"detail_url": url3}, {"_id": False}): continue title = item2.text.strip() - print(url3) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), url3) response4 = await client.get(url3) response4.encoding = "gb2312" print(f"四级连接状态:{response4.status_code}") @@ -161,24 +162,23 @@ async def getData(start_date: datetime, end_date: datetime): print( f"中国经济导报---{month.strftime('%Y-%m')}-{period_id}---{banmianming}---{banmianhao}----采集完成!") await asyncio.sleep(random.randint(5, 15)) - print( - f"中国经济导报---{month.strftime('%Y-%m')}-{period_id}-----采集完成!") + print(f"中国经济导报---{month.strftime('%Y-%m')}-{period_id}-----采集完成!") await asyncio.sleep(random.randint(5, 15)) - except Exception as e: - await collection.insert_one( - {'banmianhao': 'empty', - 'banmianming': 'empty', - 'preTitle': 'empty', - 'title': 'empty', - 'subtitle': 'empty', - 'author': 'empty', - 'keywordlist': 'empty', - 'detail_url': url, - 'release_time': month + timedelta(days=int(period_id)), - 'insert_timestamp': datetime.today(), - 'content': 'empty'} - ) - print(e) + except Exception as e: + await collection.insert_one( + {'banmianhao': 'empty', + 'banmianming': 'empty', + 'preTitle': 'empty', + 'title': 'empty', + 'subtitle': 'empty', + 'author': 'empty', + 'keywordlist': 'empty', + 'detail_url': url, + 'release_time': month + timedelta(days=int(period_id)), + 'insert_timestamp': datetime.today(), + 'content': 'empty'} + ) + print(e) print(f"中国经济导报采集完毕,共采集{crawl_num}条数据!")