知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 公眾號(hào)相關(guān) >
如何使用 Python 爬取微信公眾號(hào)文章
發(fā)表時(shí)間:2020-11-3
發(fā)布人:葵宇科技
瀏覽次數(shù):53
我比較喜歡看公眾號(hào),有時(shí)遇到一個(gè)感興趣的公眾號(hào)時(shí),都會(huì)感覺相逢恨晚,想一口氣看完所有歷史文章。但是微信的閱讀體驗(yàn)挺不好的,看歷史文章得一頁頁的往后翻,下一次再看時(shí)還得重復(fù)操作,很是麻煩。
于是便想著能不能把某個(gè)公眾號(hào)所有的文章都保存下來,這樣就很方便自己閱讀歷史文章了。
話不多說,下面我就介紹如何使用 Python 爬取微信公眾號(hào)所有文章的。
主要有以下步驟:
1 使用 Fiddler 抓取公眾號(hào)接口數(shù)據(jù)
2 使用 Python 腳本獲取公眾號(hào)所有歷史文章數(shù)據(jù)
3 保存歷史文章
Fiddler 抓包
Fiddler 是一款抓包工具,可以監(jiān)聽網(wǎng)絡(luò)通訊數(shù)據(jù),開發(fā)測(cè)試過程中非常有用,這里不多做介紹。沒有使用過的可以查看這篇文章,很容易上手。
https://blog.csdn.net/jingjingshizhu/article/details/80566191
接下來,使用微信桌面客戶端,打開某個(gè)公眾號(hào)的歷史文章,這里以我的公眾號(hào)舉例,如下圖。
如果你的 fiddler 配置好了的話,能夠看到如下圖的數(shù)據(jù)。
圖中包含抓取的 url、一些重要的參數(shù)和我們想要的數(shù)據(jù)。
這些參數(shù)中,offset
控制著翻頁,其他參數(shù)在每一頁中都是固定不變的。
接口返回的數(shù)據(jù)結(jié)構(gòu)如下圖,其中 can_msg_continue
字段控制著能否翻頁,1 表示還有下一頁,0 表示沒有已經(jīng)是最后一頁了。 next_offset
字段就是下一次請(qǐng)求的 offset
參數(shù)。
構(gòu)造請(qǐng)求,獲取數(shù)據(jù)
接下來我們的目標(biāo)就是根據(jù) url 和一些參數(shù),構(gòu)建請(qǐng)求,獲取標(biāo)題、文章 url 和日期等數(shù)據(jù),保存數(shù)據(jù)。
保存數(shù)據(jù)一種是使用 pdfkit 將 文章 url 保存為 pdf 文件;另一種是先保存 html 文件,然后將 html 制作成 chm 文件。
1 將 文章 url 保存為 pdf 文件,關(guān)鍵代碼如下:
def parse(index, biz, uin, key):
# url前綴
url = "https://mp.weixin.qq.com/mp/profile_ext"
?
# 請(qǐng)求頭
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 "
"Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.901.400 "
"QQBrowser/9.0.2524.400",
}
?
proxies = {
'https': None,
'http': None,
}
?
# 重要參數(shù)
param = {
'action': 'getmsg',
'__biz': biz,
'f': 'json',
'offset': index * 10,
'count': '10',
'is_ok': '1',
'scene': '124',
'uin': uin,
'key': key,
'wxtoken': '',
'x5': '0',
}
?
# 發(fā)送請(qǐng)求,獲取響應(yīng)
response = requests.get(url, headers=headers, params=param, proxies=proxies)
response_dict = response.json()
?
print(response_dict)
?
next_offset = response_dict['next_offset']
can_msg_continue = response_dict['can_msg_continue']
?
general_msg_list = response_dict['general_msg_list']
data_list = json.loads(general_msg_list)['list']
?
# print(data_list)
?
for data in data_list:
try:
# 文章發(fā)布時(shí)間
datetime = data['comm_msg_info']['datetime']
date = time.strftime('%Y-%m-%d', time.localtime(datetime))
?
msg_info = data['app_msg_ext_info']
?
# 文章標(biāo)題
title = msg_info['title']
?
# 文章鏈接
url = msg_info['content_url']
?
# 自己定義存儲(chǔ)路徑(絕對(duì)路徑)
pdfkit.from_url(url, 'C:/Users/admin/Desktop/wechat_to_pdf/' + date + title + '.pdf')
?
print(title + date + '成功')
?
except:
print("不是圖文消息")
?
if can_msg_continue == 1:
return True
else:
print('爬取完畢')
return False
2 保存 html 文件,關(guān)鍵代碼如下
def parse(index, biz, uin, key):
?
# url前綴
url = "https://mp.weixin.qq.com/mp/profile_ext"
?
# 請(qǐng)求頭
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 "
"Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.901.400 "
"QQBrowser/9.0.2524.400",
}
?
proxies = {
'https': None,
'http': None,
}
?
# 重要參數(shù)
param = {
'action': 'getmsg',
'__biz': biz,
'f': 'json',
'offset': index * 10,
'count': '10',
'is_ok': '1',
'scene': '124',
'uin': uin,
'key': key,
'wxtoken': '',
'x5': '0',
}
?
# 發(fā)送請(qǐng)求,獲取響應(yīng)
reponse = requests.get(url, headers=headers, params=param, proxies=proxies)
reponse_dict = reponse.json()
?
# print(reponse_dict)
next_offset = reponse_dict['next_offset']
can_msg_continue = reponse_dict['can_msg_continue']
?
general_msg_list = reponse_dict['general_msg_list']
data_list = json.loads(general_msg_list)['list']
?
print(data_list)
?
for data in data_list:
try:
datetime = data['comm_msg_info']['datetime']
date = time.strftime('%Y-%m-%d', time.localtime(datetime))
?
msg_info = data['app_msg_ext_info']
?
# 標(biāo)題
title = msg_info['title']
?
# 內(nèi)容的url
url = msg_info['content_url'].replace("\\", "").replace("http", "https")
url = html.unescape(url)
print(url)
?
res = requests.get(url, headers=headers, proxies=proxies)
with open('C:/Users/admin/Desktop/test/' + title + '.html', 'wb+') as f:
f.write(res.content)
?
print(title + date + '成功')
?
except:
print("不是圖文消息")
?
if can_msg_continue == 1:
return True
else:
print('全部獲取完畢')
return False
保存文章
保存為 pdf 文件,用到了 python 的第三方庫 pdfkit 和 wkhtmltopdf。
安裝 pdfkit:
pip install pdfkit
安裝 wkhtmltopdf:
下載地址:
https://wkhtmltopdf.org/downloads.html
安裝后將 wkhtmltopdf 目錄下的 bin 添加到環(huán)境變量中。
保存為 chm 文件,可以下載 Easy CHM ,使用這個(gè)軟件可以將 html 制作成 chm,使用教程網(wǎng)上比較多。
下載地址:
http://www.etextwizard.com/cn/easychm.html
效果圖:
pdf 和 chm 對(duì)比:
pdf 支持多終端,閱讀體驗(yàn)好,但是有個(gè)大坑,就是微信文章保存的 pdf 沒有圖片,很影響閱讀體驗(yàn),暫未找到解決辦法。
chm 的好處是可以建立索引,查看文章方便。一個(gè)公眾號(hào)制作成一個(gè) chm 文件,管理方便。不會(huì)出現(xiàn)圖片不顯示問題。
所以推薦將爬取到的公眾號(hào)文章保存為 chm 文件,方便閱讀。
相關(guān)案例查看更多
相關(guān)閱讀
- 昆明小程序開發(fā)
- 微信小程序開發(fā)入門課程
- 網(wǎng)站建設(shè)優(yōu)化
- 昆明小程序設(shè)計(jì)
- 手機(jī)網(wǎng)站建設(shè)
- 汽車報(bào)廢軟件
- 昆明網(wǎng)站開發(fā)
- 小程序生成海報(bào)
- 百度排名
- 網(wǎng)絡(luò)公司聯(lián)系方式
- 報(bào)廢車管理系統(tǒng)
- 企業(yè)網(wǎng)站
- 云南網(wǎng)站建設(shè)公司地址
- 云南網(wǎng)站建設(shè)外包
- 云南小程序公司
- 微信分銷系統(tǒng)
- 網(wǎng)絡(luò)公司
- 云南網(wǎng)站建設(shè)哪家好
- 云南網(wǎng)站建設(shè)列表網(wǎng)
- 小程序開發(fā)公司
- 云南小程序開發(fā)推薦
- 網(wǎng)站建設(shè)首選公司
- uniapp開發(fā)小程序
- 報(bào)廢車拆解系統(tǒng)
- 云南花農(nóng)小程序
- 昆明小程序開發(fā)聯(lián)系方式
- 云南小程序哪家好
- 網(wǎng)站建設(shè)公司哪家好
- 云南做軟件
- 云南網(wǎng)站建設(shè)首選