还在手动截图或复制粘贴保存微信公众号的好著作?告诉你一个更高效的秩序橘梨纱第六部,不错让你自动化将你可爱的微信公众号著作保存为PDF,而况体式不变!这不仅不错帮你放胆整理、存档著作,还能在作家删除或公众号被封时,仍是保留那些颠倒的骨子。今天,我将为各人带来一项颠覆性的技能——自动化爬取微信公众号著作并原样保留体式存储为PDF文献!从此告别繁琐,一键处分!而况是全网独此确实有用代码
一、为何需要下载微信公众号著当作PDF?
微信公众号当作咱们取得信息、学习学问的迫切渠说念,每天皆有重大的优质著作瓦解。干系词,偶而候咱们会遭遇一些不成预感的情况,比如著作被作家删除、微信公众号被封号等,导致咱们无法再次找到这些好著作。或者,长按复制联贯,翻开网页版保存,但老是少了点什么——体式、图片橘梨纱第六部、排版十足变了样。为了幸免这些缺憾,将著作下载到腹地并保存为PDF体式成为了一个很好的禁受。PDF体式具有跨平台、不易修改等特质,唐突确保著作骨子的完好性和可读性。
二、怎么收场微信公众号著作一键爬取并保存为PDF
姐妹花图片
前提准备
1.注册一个个东说念主微信公众号用来取得思要爬取的公众号的始终地址
注册秩序:《https://kf.qq.com/faq/120911VrYVrA151009eIrYvy.html》
2.准备python环境并装配相应的第三方库
pip install requests # 这是爬虫必备库pip install pdfkit # 用于转pdf的器具库,有一个exe运用秩序,# 不错后台私信取得pip install wechatsogou # 取得每一篇著作的api接口pip uninstall Werkzeug # wechatsogou 依赖库默许是3.0.3版块,然而会报多样罪戾pip3 install Werkzeug==0.11.15 # 装配正确版块3.下载软件wkhtmltox 我会放到网盘里,后台私信'pachong'取得下载联贯
图片
批量取得微信公众号往期推送的URL、主题和期间橘梨纱第六部
参加微信公众号——左侧草稿箱——新的创作—— 在图文剪辑页中禁受超联贯——搜索你思要爬取的微信公众堪称呼——F12找到苦求联贯——复制curl bash——在curlconvert网站中取得苦求地址和苦求参数——瓦解取得的遵循,得到著作标题、著作期间和著作URL
图片
图片
图片
图片
图片
图片
图片
图片
import requestsimport jsonimport timedef crawl_all_article_url(page): cookies = { 'appmsglist_action_3948617697': 'card', 'ua_id': 'cPS3GpAA01IEkaRJAAAAAIvDxBMaEL0YwwkNCjBwcAQ=', 'wxuin': '00617100352084', 'mm_lang': 'zh_CN', 'pac_uid': '0_6f1a31a07c4ab', 'iip': '0', '_qimei_uuid42': '17c1a0b23191007d54d4e4bc638bde211f0338f3bf', '_qimei_q36': '', '_qimei_h38': '8f95287254d4e4bc638bde2102000002517c1a', 'RK': 'jTHYN6T3Wp', 'ptcz': '7345f2bcdd021ede2b9e3b5ff3857a614e8f78a9ecdf01650aa9a3a225afd21e', 'pgv_pvid': '8536395802', 'ts_uid': '6019633394', '__root_domain_v': '.weixin.qq.com', '_qddaz': 'QD.243806759066417', 'qq_domain_video_guid_verify': '6e9c8245aad0e837', '_qimei_fingerprint': '36d8e5c4ef2b920cd2da41f5f01ca167', '_clck': '3902705164|1|fm1|0', 'uuid': '4f9478c2739d6acbb0878a2e7b6c7b35', 'rand_info': 'CAESINjnm2X0Xw6se5Kiw4ayZI9Ft4uFGZEaPM8tGhG8HhAl', 'slave_bizuin': '3902705164', 'data_bizuin': '3902705164', 'bizuin': '3902705164', 'data_ticket': 'DJqUc+FKg56UrvBm8mHaCSI28Je1eBjufcd/Bb1OMvhzcoXTTFoXRMrQIbNGjKtA', 'slave_sid': 'cG5yOWQzc0JrX3BNejZmMEhZWG5yN1JFZ1lzMzVWbW85aDZVRHpuSmw2UGVyaTczRFozdGFDMWpvd2ZQckhMbm5tX0pNc1FkcWNBX3VnbEFDZ29pZ3JLNW5hTEdqbm9ROXIyX01GMW5TeUYzQ2RhbUdoZmtldTJDS3N4eWlDYmR0MUd0ZUtqcmkxYjV0UGZj', 'slave_user': 'gh_19182aefb42a', 'xid': '081913df3456544c4dc84a51fd52778f', '_clsk': '121cm1q|1716519674628|15|1|mp.weixin.qq.com/weheat-agent/payload/record',} headers = { 'accept': '*/*', 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8', # 'cookie': 'appmsglist_action_3948617697=card; ua_id=cPS3GpAA01IEkaRJAAAAAIvDxBMaEL0YwwkNCjBwcAQ=; wxuin=00617100352084; mm_lang=zh_CN; pac_uid=0_6f1a31a07c4ab; iip=0; _qimei_uuid42=17c1a0b23191007d54d4e4bc638bde211f0338f3bf; _qimei_q36=; _qimei_h38=8f95287254d4e4bc638bde2102000002517c1a; RK=jTHYN6T3Wp; ptcz=7345f2bcdd021ede2b9e3b5ff3857a614e8f78a9ecdf01650aa9a3a225afd21e; pgv_pvid=8536395802; ts_uid=6019633394; __root_domain_v=.weixin.qq.com; _qddaz=QD.243806759066417; qq_domain_video_guid_verify=6e9c8245aad0e837; _qimei_fingerprint=36d8e5c4ef2b920cd2da41f5f01ca167; _clck=3902705164|1|fm1|0; uuid=4f9478c2739d6acbb0878a2e7b6c7b35; rand_info=CAESINjnm2X0Xw6se5Kiw4ayZI9Ft4uFGZEaPM8tGhG8HhAl; slave_bizuin=3902705164; data_bizuin=3902705164; bizuin=3902705164; data_ticket=DJqUc+FKg56UrvBm8mHaCSI28Je1eBjufcd/Bb1OMvhzcoXTTFoXRMrQIbNGjKtA; slave_sid=cG5yOWQzc0JrX3BNejZmMEhZWG5yN1JFZ1lzMzVWbW85aDZVRHpuSmw2UGVyaTczRFozdGFDMWpvd2ZQckhMbm5tX0pNc1FkcWNBX3VnbEFDZ29pZ3JLNW5hTEdqbm9ROXIyX01GMW5TeUYzQ2RhbUdoZmtldTJDS3N4eWlDYmR0MUd0ZUtqcmkxYjV0UGZj; slave_user=gh_19182aefb42a; xid=081913df3456544c4dc84a51fd52778f; _clsk=121cm1q|1716519674628|15|1|mp.weixin.qq.com/weheat-agent/payload/record', 'referer': 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=77&createType=0&token=1635832127&lang=zh_CN×tamp=1716519674285', 'sec-ch-ua': ''Google Chrome';v='123', 'Not:A-Brand';v='8', 'Chromium';v='123'', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': ''Windows'', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36', 'x-requested-with': 'XMLHttpRequest', } params = { 'sub': 'list', 'search_field': 'null', 'begin': f'{5*page}', 'count': '5', 'query': '', 'fakeid': 'MzkyMjY2MjMwOQ==', 'type': '101_1', 'free_publish_type': '1', 'sub_action': 'list_ex', 'token': '1635832127', 'lang': 'zh_CN', 'f': 'json', 'ajax': '1', } print(params) response = requests.get('https://mp.weixin.qq.com/cgi-bin/appmsgpublish', params=params, cookies=cookies, headers=headers) resp = response.json()['publish_page'] # print(response.json()) resp = json.loads(resp) print(len(resp['publish_list'])) for i in resp['publish_list']: article_links = [] art_info = json.loads(i['publish_info']) art_title = art_info['appmsgex'][0]['title'] art_time = art_info['appmsgex'][0]['create_time'] art_date = date = time.strftime('%Y-%m-%d', time.localtime(art_time)) art_link = art_info['appmsgex'][0]['link'] a_info = { 'url': art_link, 'title': art_title, 'time': art_date } article_links.append(a_info) return article_linksarticle_links= []for i in range(7): # 7是微信公众号著作页数 page_link = crawl_all_article_url(i) print(page_link) article_links = article_links + page_link time.sleep(5)
图片
爬取扫数著作并存储为PDF体式
得到著作扫数的url之后下一步等于爬取著作的具体骨子了,在这里要用到wechatsougo接口,径直pip install 装配会存在依赖库版块问题,主淌若库Werkzeug 的版块不合,老是报错,我在这个方位卡了两天,直到试到版块Werkzeug==0.11.15 才告捷,需要你作念的是:
pip uninstall Werkzeug # wechatsogou 依赖库默许是3.0.3版块,然而会报多样罪戾pip3 install Werkzeug==0.11.15 # 装配正确版块import pdfkitimport wechatsogoudef link_to_pdf(url, title, date): ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3) # 调用接口 content_info = ws_api.get_article_content(url) # 苦求联贯 content = content_info['content_html'] # 调养为html体式 # 将标题和著作骨子放入如下html中处理一下 html = f''' <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <title>{title}</title> </head> <body> <h2 style='text-align: center;font-weight: 400;'>{title}</h2> {content} </body> </html>''' # wkhtmltopdf可实施秩序旅途 path_wkthmltopdf = r'D:\software-dir\wkhtmltox\bin\wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) # 成立pdfkit pdfkit.from_string(html, f'{title} {date}.pdf', configuration=config) # 转PDF,并按设定好的定名文献 print(f'{title}.pdf 已下载') for link in article_links: url = link['url'] title = link['title'] date = link['time'] link_to_pdf(url, title, date)
图片
图片
图片
本站仅提供存储做事,扫数骨子均由用户发布,如发现存害或侵权骨子,请点击举报。