📚博客主頁:knighthood2001
?公眾號:認知up吧 (目前正在帶領大家一起提升認知,感興趣可以來圍觀一下)
🎃知識星球:【認知up吧|成長|副業】介紹
??感謝大家點贊👍🏻收藏?評論?🏻,您的三連就是我持續更新的動力??
🙏筆者水平有限,歡迎各位大佬指點,相互學習進步!
看文檔!看文檔!看文檔!
https://developers.weixin.qq.com/doc/offiaccount/Publish/Get_publication_records.html
我本來想通過微信公眾平臺的接口,獲取群發的公眾號文章鏈接,但是結果如下:
本應該返回包含文章鏈接的內容,但是是個空。
經過多次測試,我放棄了,然后在網上查了一下,說是沒有獲取群發文章列表的API。
因此這篇文章,就算是廢了。只能用來記錄一下,如何看官方文檔,然后寫例子。
獲取access_token
Python:獲取微信公眾號的access_token
獲取公眾號的access_token
可以看上面這一篇。
這篇文章對獲取access_token
進行封裝了一下,因為你需要判斷是否會出現其他錯誤,因此你需要if去判斷。
APPID = "你的APPID"
APPSECRET = "你的APPSECRET"
def get_access_token():# 構造請求的URLurl = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}"response = requests.get(url)return_json = response.json()if 'errcode' in return_json:if return_json["errcode"] == '40164':print("IP白名單未配置:", return_json["errmsg"])return Noneelse:print("獲取access_token失敗,報錯信息:", return_json["errmsg"])return Noneelse:access_token = response.json()["access_token"]return access_token
比如下面的第一行,就是這個代碼打印的,可以方便大家看沒有配置的IP(不需要IP工具去查找本機IP)
上面這個函數成功運行后,不出其他錯誤,你就能得到access_token
。
獲取穩定的access_token
獲取穩定的access_token
,其實反而更加簡單。
只需要構造一個請求體。
def get_stable_access_token():# 構造請求的URLurl = "https://api.weixin.qq.com/cgi-bin/stable_token"data = {"grant_type": "client_credential","appid": APPID,"secret": APPSECRET,"force_refresh": False}response = requests.post(url, data=json.dumps(data))access_token = response.json()["access_token"]print('access_token:', access_token)return access_token
獲取文章列表
然后看一下微信公眾平臺開放文檔。
可以看到,需要access_token
、offset
、count
。
但是,微信公眾平臺的API要求指定獲取素材的類型。
type
字段用于告訴微信服務器你想要獲取哪種類型的素材。在這個上下文中,news
表示你想要獲取圖文消息列表。
微信公眾平臺提供了多種類型的素材,包括
圖文(news)
、圖片(image)
、語音(voice)
、視頻(video)
等。當你調用接口時,你需要明確指定你想要獲取的素材類型,這樣微信服務器才能返回正確的數據。
access_token
是需要放在API里面。
def get_article_list(count=10):access_token = get_access_token()# access_token = get_stable_access_token()print('access_token:', access_token)# 構造獲取文章列表的URLarticle_url = f"https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={access_token}"# 構造請求體data = {"type": "news","offset": 0,"count": count}# 發送POST請求獲取文章列表response = requests.post(article_url, json=data)# response = requests.post(article_url, data=json.dumps(data))print(response.json())# 解析返回的JSON數據# articles = response.json()["item"]# return articles
因此,我構造了這樣一個函數。
首先就是構造API網址。將access_token
加進去。
article_url = f"https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={access_token}"
然后構造請求體
data里面就是你需要放的參數,參數對了才可以得到指定的內容。count
就是你要獲取的文章數量。
然后發送post
請求。下面這兩種方式都是可以的。
response = requests.post(article_url, json=data)
response = requests.post(article_url, data=json.dumps(data))
json.dumps
是 Python 的json
模塊中的一個函數,用于將 Python 對象轉換(或序列化)為JSON
格式的字符串。這個函數非常有用,當你需要將 Python 數據結構(如字典、列表、元組等)轉換為JSON
格式的文本時,你可以使用它。
全文的代碼
import requests
import json
APPID = "wx465ccee8c1ea66f5"
APPSECRET = "b9d387d8ada1e82a69c809b7fc8ea2e7"# APPID = "你的APPID"
# APPSECRET = "你的APPSECRET"
def get_access_token():# 構造請求的URLurl = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}"response = requests.get(url)return_json = response.json()if 'errcode' in return_json:if return_json["errcode"] == '40164':print("IP白名單未配置:", return_json["errmsg"])return Noneelse:print("獲取access_token失敗,報錯信息:", return_json["errmsg"])return Noneelse:access_token = response.json()["access_token"]return access_token# access_token = "access_token 80_U6eqICBzuXntzzICOHBEQrKe4n4lH2MtMn-69791Kx1fjsQ28V2RkpSIuGrbhA2LKC2iGIAVCHvLE30k8Dli-Q3try69bR0UQihKi7hO_aIG0Q5HRI_kxAZcfOIFITdABAMPA"def get_article_list(count=10):access_token = get_access_token()# access_token = get_stable_access_token()print('access_token:', access_token)# 構造獲取文章列表的URLarticle_url = f"https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={access_token}"# 構造請求體data = {"type": "news","offset": 0,"count": count}# 發送POST請求獲取文章列表response = requests.post(article_url, json=data)# response = requests.post(article_url, data=json.dumps(data))print(response.json())# 解析返回的JSON數據# articles = response.json()["item"]# return articlesdef get_stable_access_token():"""獲取穩定的access_tokenArgs:無Returns:str: 返回穩定的access_token"""# 構造請求的URLurl = "https://api.weixin.qq.com/cgi-bin/stable_token"data = {"grant_type": "client_credential","appid": APPID,"secret": APPSECRET,"force_refresh": False}response = requests.post(url, data=json.dumps(data))access_token = response.json()["access_token"]print('access_token:', access_token)return access_token
if __name__ == '__main__':# get_access_token()# 指定公眾號名稱和要獲取的文章數量count = 10# 獲取文章列表# articles = get_article_list(count)get_stable_access_token()
運行結果如下,就是返回結果不像開發文檔里面說的,有相關內容,這里其實根本沒有內容。
總結
本來還想通過接口獲取公眾號文章鏈接,但是這種方法行不通了,后續我打算使用selenium進行獲取。