
天貓商品數(shù)據(jù)爬取方案:官方API與非官方接口實(shí)戰(zhàn)
配置IP 白名單
在公眾號(hào)后臺(tái)的「設(shè)置與開(kāi)發(fā)」-「基本配置」,“IP白名單”設(shè)置即可,把你的ip(執(zhí)行api調(diào)用的機(jī)器ip)添加進(jìn)去。沒(méi)有添加,發(fā)起請(qǐng)求會(huì) “errcode: 40164,errmsg: invalid ip”。
接口封裝發(fā)布文章一共就四步:獲取token、上傳封面、保持草稿、發(fā)布。
幾乎所有的接口都用token做身份認(rèn)證的,接口功能調(diào)用的第一步。
參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
調(diào)用接口為:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={yourappid}&secret={yoursecret}
用postman來(lái)做請(qǐng)求測(cè)試:
代碼:(去除了日志信息,下同)
主要是后面的操作,需要一個(gè)圖片作為封面,所以需要提前上傳一個(gè)圖片資源,對(duì)應(yīng)的接口就是“新增永久素材”了
參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
調(diào)用接口:
https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type=image
代碼:
提示:如果多次發(fā)布文章,使用同樣的封面,則可以先調(diào)用這個(gè)接口,把media_id 保存下來(lái),之后這步就不是必要的了,后面新建草稿時(shí)候,直接用寫定的media_id就可。
參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Draft_Box/Add_draft.html
調(diào)用接口:
https://api.weixin.qq.com/cgi-bin/draft/add?access_token={access_token}
填上我們關(guān)注的必填選就可,需要填寫的不多。
代碼:
注意:如果發(fā)布后,內(nèi)容有亂碼,如:
那是編碼問(wèn)題,構(gòu)造json 數(shù)據(jù)時(shí)候,ensure_ascii 用false。上例是python代碼,如果json.dumps(drafts)會(huì)出錯(cuò)。需要改為:json.dumps(drafts, ensure_ascii=False).encode(‘utf-8’) 。其他語(yǔ)言也有對(duì)應(yīng)的設(shè)置方法。
最后一步就是發(fā)布了,這個(gè)接口最為簡(jiǎn)單。直接把前面的增加草稿返回的media_id 提交下就可。
參考文檔:https://developers.weixin.qq.com/doc/offiaccount/Publish/Publish.html
調(diào)用接口:
https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token={access_token}
代碼:
這步不是必要的,不關(guān)注發(fā)布狀態(tài)的就可以無(wú)視。由于發(fā)布文章是異步的,同時(shí)發(fā)布后的文章可能還被內(nèi)容審核啥的,所以發(fā)布后的文章,可以通過(guò)publish_id 去請(qǐng)求“發(fā)布狀態(tài)輪詢接口”接口查詢狀態(tài)。
整合發(fā)表功能基本函數(shù)封裝好之后,發(fā)表文章就是一步到位了:
特別提醒:現(xiàn)在公眾號(hào)是推薦制度,測(cè)試結(jié)果:通過(guò)api發(fā)布的文章,不會(huì)觸發(fā)微信系統(tǒng)推薦,也不會(huì)在主頁(yè)顯示,可用于菜單或者鏈接分享方式給用戶閱讀。
#-*- coding: UTF-8 -*-
import json
import os
import requests
def _str_of_json(content, key):
res = json.loads(str(content, 'utf8'))
if key not in res or res[key] == "":
print(res)
return False, ""
return True, res[key]
# 獲取token 返回(sucess, token)
def get_token(appid, secret):
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % (appid, secret)
res = requests.get(url)
return _str_of_json(res.content, "access_token")
# 上傳圖片 返回(sucess, media_id)
def upload_image(token, file_path):
url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=image" % token
with open(file=file_path, mode='rb') as fp:
files = {'media': fp}
resp = requests.post(url, files=files)
return _str_of_json(resp.content, "media_id")
return False, ""
# 新建草稿 返回(sucess, media_id)
def add_draft(token, cover_media_id, title, content):
url = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token=%s" % token
article = {
"title":title,
"thumb_media_id":cover_media_id,
"content":content,
# "author":"author", # 非必填
# "digest":"digest", # 非必填
}
drafts = {
"articles":[
article
]
}
headers = {'Content-Type': 'application/json'}
# 直接post json.dumps(drafts) 會(huì)有亂碼,所以
post_data = json.dumps(drafts, ensure_ascii=False).encode('utf-8')
res = requests.post(url, data=post_data, headers=headers)
return _str_of_json(res.content, "media_id")
# 發(fā)布,返回(sucess, publish_id)
def publish(token, draft_id):
url = "https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token=%s" % token
res = requests.post(url, json={"media_id":draft_id})
return _str_of_json(res.content, "publish_id")
def publish_article(appid, secret, title, content, cover_file_path):
# 1 獲取token
res, token = get_token(appid, secret)
# 2 上傳封面
res, cover_media_id = upload_image(token, cover_file_path)
# 3 保存草稿
ret, draft_id = add_draft(token, cover_media_id, title, content)
# 4 發(fā)布
ret, publish_id = publish(token, draft_id)
if __name__ == '__main__':
appid = "wx000000000000" # 你的 appid
secret = "xxxxxxxxxxxxxxx" # 你的 secret
file_path = "/Users/xxx/yyyy.jpg" # 你的文章封面圖片
title = "你的文章標(biāo)題"
content = "你的文章內(nèi)容"
publish_article(appid, secret, title, content, file_path)
原文轉(zhuǎn)載自:https://mp.weixin.qq.com/s/vu9Aep-QBwN0xu4i28W3UA
天貓商品數(shù)據(jù)爬取方案:官方API與非官方接口實(shí)戰(zhàn)
地圖開(kāi)發(fā)者平臺(tái)對(duì)比:高德、百度、騰訊、必應(yīng)、天地圖等API
讓大模型“聯(lián)網(wǎng)”的第一步?手把手教你調(diào)用搜索API!
API接口安全—webservice、Swagger、WEBpack
從零開(kāi)始認(rèn)識(shí) API,讓網(wǎng)頁(yè)信息成為你的「知識(shí)庫(kù)」
APISIX-MCP:利用 AI + MCP 擁抱 API 智能化管理
如何0代碼將存量 API 適配 MCP 協(xié)議?
C# 與 Windows API 交互的“秘密武器”:結(jié)構(gòu)體和聯(lián)合體
免費(fèi)強(qiáng)大的API開(kāi)發(fā)和調(diào)試工具——Reqable
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)