import json
from urllib.parse import urlencode
import requests
def call_api_web_server(req_type, req_data):
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json;charset=UTF-8",
"Pragma": "no-cache",
}
api_url = "https://api.addcoder.com/" + req_type
req_params = {"api_auth_key": "601137664d318ffde7c095381db55434"}
response = requests.post(
api_url,
headers=headers,
params=req_params,
data=json.dumps(req_data, separators=(",", ":")),
timeout=20,
)
if response.json().get("respCode") == 0:
return response.json()["respData"]
else:
print(
"[api]-[error]-[%s]-[%s]"
% (response.json().get("respMsg"), response.json().get("error"))
)
if __name__ == "__main__":
# 搜索
params = {
"device_platform": "webapp",
"aid": "6383",
"channel": "channel_pc_web",
"keyword": "天才小熊猫",
"search_source": "normal_search",
"webid": "7062613290769647144",
}
info = {
"url": "https://www.douyin.com/aweme/v1/web/general/search/single/?"
+ urlencode(params),
"method": "GET",
}
respData = call_api_web_server("douyin_web_xhr", info)
req_url = respData["data"]
print(req_url)
# 用户
params = {
"device_platform": "webapp",
"aid": "6383",
"channel": "channel_pc_web",
"sec_user_id": "MS4wLjABAAAAbV4b87zK6ATbJMwFHE7dvhEQ2S78UK26Dtr9a5UzzUw",
"webid": "7062602529569506816",
}
info = {
"url": "https://www.douyin.com/aweme/v1/web/user/profile/other/?"
+ urlencode(params),
"method": "GET",
}
respData = call_api_web_server("douyin_web_xhr", info)
req_url = respData["data"]
print(req_url)