From 9b4c9ad6360c7ea1d7e4391fab0a281220a8a582 Mon Sep 17 00:00:00 2001 From: FW27623 <1970446279@qq.com> Date: Thu, 6 Jun 2024 20:36:47 +0800 Subject: [PATCH] Update qqwry.py --- qqwry.py | 63 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/qqwry.py b/qqwry.py index d5cbf79..087160c 100644 --- a/qqwry.py +++ b/qqwry.py @@ -2,44 +2,57 @@ import re import json import requests from bs4 import BeautifulSoup +import time -def get_link(url): +def get_link(url, retries=3, delay=5): headers = { 'Accept-Language': 'zh-CN,zh;q=0.9,en-CN;q=0.8,en;q=0.7,zh-TW;q=0.6', 'Cookie': 'rewardsn=; wxtokenkey=777', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36' } - # 访问链接并从json中提取微信推文链接 - response = requests.get(url, headers=headers) - data = json.loads(response.text) - article_list = data['getalbum_resp']['article_list'] + for attempt in range(retries): + try: + response = requests.get(url, headers=headers) + response.raise_for_status() + data = json.loads(response.text) + article_list = data['getalbum_resp']['article_list'] - for article in article_list: - title = article['title'] - if "纯真IP库社区版更新" in title: - link = article['url'] - zip_url = get_zip_url(link) - if zip_url: - return link + for article in article_list: + title = article['title'] + if "纯真IP库社区版更新" in title: + link = article['url'] + zip_url = get_zip_url(link, retries, delay) + if zip_url: + return link + return None + except (requests.RequestException, ValueError) as e: + print(f"第{attempt+1}次请求失败:{e}") + if attempt < retries - 1: + time.sleep(delay) return None -def get_zip_url(link): - # 访问微信推文链接并解析网页 - response = requests.get(link) - soup = BeautifulSoup(response.text, 'html.parser') +def get_zip_url(link, retries=3, delay=5): + for attempt in range(retries): + try: + response = requests.get(link) + response.raise_for_status() + soup = BeautifulSoup(response.text, 'html.parser') - # 提取文本中的zip链接,正则匹配以https://开头以.zip后缀的链接 - content_div = soup.find('div', {'id': 'js_content'}) - if content_div is not None: - content = content_div.get_text() - zip_url = re.findall(r'https://.*?\.zip', content) - return zip_url - else: - return None + content_div = soup.find('div', {'id': 'js_content'}) + if content_div is not None: + content = content_div.get_text() + zip_url = re.findall(r'https://.*?\.zip', content) + return zip_url + else: + return None + except (requests.RequestException, ValueError) as e: + print(f"第{attempt+1}次请求失败:{e}") + if attempt < retries - 1: + time.sleep(delay) + return None if __name__ == '__main__': - # 从微信推文json数据中获得最新一期IP库的发布文章链接 url = 'https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg3Mzc0NTA3NA==&action=getalbum&album_id=2329805780276838401&f=json' try: