def getLink(url:str): """ :param url: :return: """ 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) link = data['getalbum_resp']['article_list'][0]['url'] return link def getZipUrl(link:str): """ :param link: :return: """ # 访问微信推文链接并解析网页 response = requests.get(link) soup = BeautifulSoup(response.text, 'html.parser') # 提取文本中的zip链接,正则匹配以https://开头以.zip后缀的链接 content = soup.find('div', {'id': 'js_content'}).get_text() zipurl = re.findall(r'https://.*?\.zip', content) return zipurl def requestsDownload(url:str,newfile:str): """ :param url: :param newfile: :return: """ content = requests.get(url).content with open(newfile, 'wb') as file: file.write(content) def print_hi(name): # Use a breakpoint in the code line below to debug your script. print(f'Hi, {name} world,geovindu,涂聚文') # Press Ctrl+F8 to toggle the breakpoint. # Press the green button in the gutter to run the script. if __name__ == '__main__': #print_hi('PyCharm,python language') # 从微信推文json数据中获得最新一期IP库的发布文章链接 url = 'https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg3Mzc0NTA3NA==&action=getalbum&album_id=2329805780276838401&f=json' downurl="" try: link = getLink(url) if link: zip_url = getZipUrl(link) if len(zip_url)>0: for i in range(0,len(zip_url)): downurl=zip_url[0] print(zip_url[0]) else: print("没有找到zip链接") else: print("没有找到微信推文链接") except Exception as e: print("出现错误:", e) requestsDownload(downurl,"geovindu.zip")
标签:zip,Python,print,content,url,link,file,download,链接 From: https://www.cnblogs.com/geovindu/p/18166637