如何通過50行Python代碼獲取公眾號(hào)全部文章
前言
我們平時(shí)閱讀公眾號(hào)的文章會(huì)遇到一個(gè)問題——閱讀歷史文章體驗(yàn)不好。
我們知道爬取公眾號(hào)的方式常見的有兩種:通過搜狗搜索去獲取,缺點(diǎn)是只能獲取最新的十條推送文章。通過微信公眾號(hào)的素材管理,獲取公眾號(hào)文章。缺點(diǎn)是需要申請自己的公眾號(hào)。
今天介紹一種通過抓包PC端微信的方式去獲取公眾號(hào)文章的方法。相比其他的方法非常方便。
如上圖,通過抓包工具獲取微信的網(wǎng)絡(luò)信息請求,我們發(fā)現(xiàn)每次下拉刷新文章的時(shí)候都會(huì)請求 mp.weixin.qq.com/mp/xxx (公眾號(hào)不讓添加主頁鏈接,xxx表示profile_ext) 這個(gè)接口。
經(jīng)過多次測試分析,用到了以下幾個(gè)參數(shù)
- __biz : 用戶和公眾號(hào)之間的唯一id
- uin :用戶的私密id
- key :請求的秘鑰,一段時(shí)候只會(huì)就會(huì)失效
- offset :偏移量
- count :每次請求的條數(shù)
數(shù)據(jù)如下
{ "ret": 0, "errmsg": "ok", # 請求狀態(tài) "msg_count": 10, # 信息條數(shù) "can_msg_continue": 1, # 是否還可以繼續(xù)獲取,1代表可以。0代表不可以,也就是最后一頁 "general_msg_list": "{"list":[]}", # 公眾號(hào)文本信息 "next_offset": 20, "video_count": 1, "use_video_tab": 1, "real_type": 0, "home_page_list": [] }
部分代碼如下
params = { '__biz': biz, 'uin': uin, 'key': key, 'offset': offset, 'count': count, 'action': 'getmsg', 'f': 'json' } headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36' } response = requests.get(url=url, params=params, headers=headers) resp_json = response.json() if resp_json.get('errmsg') == 'ok': resp_json = response.json() # 是否還有分頁數(shù)據(jù), 用于判斷return的值 can_msg_continue = resp_json['can_msg_continue'] # 當(dāng)前分頁文章數(shù) msg_count = resp_json['msg_count'] general_msg_list = json.loads(resp_json['general_msg_list']) list = general_msg_list.get('list') print(list, "**************")
最后打印的list就是公眾號(hào)的文章信息詳情。包括標(biāo)題(titile)、摘要(digest)、文章地址(content_url)、閱讀原文地址(source_url)、封面圖(cover)、作者(author)等等...
輸出結(jié)果如下:
[{ "comm_msg_info": { "id": 1000000038, "type": 49, "datetime": 1560474000, "fakeid": "3881067844", "status": 2, "content": "" }, "app_msg_ext_info": { "title": "入門爬蟲,這一篇就夠了!?。?, "digest": "入門爬蟲,這一篇就夠了?。?!", "content": "", "fileid": 0, "content_url": "http:XXXXXX", "source_url": "", "cover": "I5kME6BVXeLibZDUhsiaEYiaX7zOoibxa9sb4stIwrfuqID5ttmiaoVAFyxKF6IjOCyl22vg8n2NPv98ibow\/0?wx_fmt=jpeg", "subtype": 9, "is_multi": 0, "multi_app_msg_item_list": [], "author": "Python3X", "copyright_stat": 11, "duration": 0, "del_flag": 1, "item_show_type": 0, "audio_fileid": 0, "play_url": "", "malicious_title_reason_id": 0, "malicious_content_type": 0 } },{...},{...},{...},{...},{...},{...},{...},{...},{...}]
獲取數(shù)據(jù)之后,可以保存到數(shù)據(jù)庫中,也可以將文章保存在PDF中。
1、保存在Mongo中
# Mongo配置 conn = MongoClient('127.0.0.1', 27017) db = conn.wx #連接wx數(shù)據(jù)庫,沒有則自動(dòng)創(chuàng)建 mongo_wx = db.article #使用article集合,沒有則自動(dòng)創(chuàng)建 for i in list: app_msg_ext_info = i['app_msg_ext_info'] # 標(biāo)題 title = app_msg_ext_info['title'] # 文章地址 content_url = app_msg_ext_info['content_url'] # 封面圖 cover = app_msg_ext_info['cover'] # 發(fā)布時(shí)間 datetime = i['comm_msg_info']['datetime'] datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(datetime)) mongo_wx.insert({ 'title': title, 'content_url': content_url, 'cover': cover, 'datetime': datetime })
結(jié)果如下
2、導(dǎo)入到PDF文件中
Python3中常用的操作PDF的庫有python-pdf和pdfkit。我用了pdfkit這個(gè)模塊導(dǎo)出pdf文件。
pdfkit是工具包Wkhtmltopdf的封裝類,因此需要安裝Wkhtmltopdf才能使用。
可以訪問 https://wkhtmltopdf.org/downloads.html 下載和操作系統(tǒng)匹配的工具包。
實(shí)現(xiàn)代碼也比較簡單,只需要傳入導(dǎo)入文件的url即可。
安裝pdfkit庫
pip3 install pdfkit -i http://pypi.douban.com/simple --trusted-host pypi.douban.com import pdfkit pdfkit.from_url('公眾號(hào)文章地址', 'out.pdf')
運(yùn)行之后成功導(dǎo)出pdf文件。
完整代碼
import requests import json import time from pymongo import MongoClient url = 'http://mp.weixin.qq.com/mp/xxx'(公眾號(hào)不讓添加主頁鏈接,xxx表示profile_ext) # Mongo配置 conn = MongoClient('127.0.0.1', 27017) db = conn.wx #連接wx數(shù)據(jù)庫,沒有則自動(dòng)創(chuàng)建 mongo_wx = db.article #使用article集合,沒有則自動(dòng)創(chuàng)建 def get_wx_article(biz, uin, key, index=0, count=10): offset = (index + 1) * count params = { '__biz': biz, 'uin': uin, 'key': key, 'offset': offset, 'count': count, 'action': 'getmsg', 'f': 'json' } headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36' } response = requests.get(url=url, params=params, headers=headers) resp_json = response.json() if resp_json.get('errmsg') == 'ok': resp_json = response.json() # 是否還有分頁數(shù)據(jù), 用于判斷return的值 can_msg_continue = resp_json['can_msg_continue'] # 當(dāng)前分頁文章數(shù) msg_count = resp_json['msg_count'] general_msg_list = json.loads(resp_json['general_msg_list']) list = general_msg_list.get('list') print(list, "**************") for i in list: app_msg_ext_info = i['app_msg_ext_info'] # 標(biāo)題 title = app_msg_ext_info['title'] # 文章地址 content_url = app_msg_ext_info['content_url'] # 封面圖 cover = app_msg_ext_info['cover'] # 發(fā)布時(shí)間 datetime = i['comm_msg_info']['datetime'] datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(datetime)) mongo_wx.insert({ 'title': title, 'content_url': content_url, 'cover': cover, 'datetime': datetime }) if can_msg_continue == 1: return True return False else: print('獲取文章異常...') return False if __name__ == '__main__': biz = 'Mzg4MTA2Nzg0NA==' uin = 'NDIyMTI5NDM1' key = '20a680e825f03f1e7f38f326772e54e7dc0fd02ffba17e92730ba3f0a0329c5ed310b0bd55b3c0b1f122e5896c6261df2eaea4036ab5a5d32dbdbcb0a638f5f3605cf1821decf486bb6eb4d92d36c620' index = 0 while 1: print(f'開始抓取公眾號(hào)第{index + 1} 頁文章.') flag = get_wx_article(biz, uin, key, index=index) # 防止和諧,暫停8秒 time.sleep(8) index += 1 if not flag: print('公眾號(hào)文章已全部抓取完畢,退出程序.') break print(f'..........準(zhǔn)備抓取公眾號(hào)第{index + 1} 頁文章.')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python+pywinauto+lackey實(shí)現(xiàn)PC端exe自動(dòng)化的示例代碼
- PythonPC客戶端自動(dòng)化實(shí)現(xiàn)原理(pywinauto)
- python自動(dòng)化工具之pywinauto實(shí)例詳解
- Python 微信公眾號(hào)文章爬取的示例代碼
- 基于Python采集爬取微信公眾號(hào)歷史數(shù)據(jù)
- python如何導(dǎo)出微信公眾號(hào)文章方法詳解
- Python selenium爬取微信公眾號(hào)文章代碼詳解
- Python版實(shí)現(xiàn)微信公眾號(hào)掃碼登陸
- Python如何爬取微信公眾號(hào)文章和評論(基于 Fiddler 抓包分析)
- python抓取搜狗微信公眾號(hào)文章
- python爬取微信公眾號(hào)文章的方法
- python使用pywinauto驅(qū)動(dòng)微信客戶端實(shí)現(xiàn)公眾號(hào)爬蟲
相關(guān)文章
python畫圖把時(shí)間作為橫坐標(biāo)的方法
今天小編就為大家分享一篇python畫圖把時(shí)間作為橫坐標(biāo)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07解決python -m pip install --upgrade pip 升級(jí)不成功問題
這篇文章主要介紹了python -m pip install --upgrade pip 解決升級(jí)不成功問題,需要的朋友可以參考下2020-03-03Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Python查找不限層級(jí)Json數(shù)據(jù)中某個(gè)key或者value的路徑方式
今天小編就為大家分享一篇Python查找不限層級(jí)Json數(shù)據(jù)中某個(gè)key或者value的路徑方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02uwsgi啟動(dòng)django項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了uwsgi啟動(dòng)django項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Python內(nèi)置函數(shù)memoryview()的實(shí)現(xiàn)示例
本文主要介紹了Python內(nèi)置函數(shù)memoryview()的實(shí)現(xiàn)示例,它允許你在不復(fù)制其內(nèi)容的情況下操作同一個(gè)數(shù)組的不同切片,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05Python高級(jí)特性——詳解多維數(shù)組切片(Slice)
今天小編就為大家分享一篇Python高級(jí)特性——詳解多維數(shù)組切片(Slice),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python中if __name__ == "__main__"詳細(xì)解釋
這篇文章主要介紹了Python中if __name__ == "__main__"詳細(xì)解釋,需要的朋友可以參考下2014-10-10Python利用memory_profiler實(shí)現(xiàn)內(nèi)存分析
memory_profiler是第三方模塊,用于監(jiān)視進(jìn)程的內(nèi)存消耗以及python程序內(nèi)存消耗的逐行分析。本文將利用memory_profiler實(shí)現(xiàn)內(nèi)存分析,需要的可以參考一下2022-10-10