API返回空值的原因是什么?
为什么代码获取不到API返回的数据?
import random,requests,jsonip = [
"49.104.25.257",
"39.115.131.116"
]
api = "http://ip-api.com/batch?fields=58898?lang=zh_CN"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
"content-type":"application/json"
}
ip = json.dumps(ip,ensure_ascii=False, indent=2)
print(ip)
resp = requests.post(url=api,data=ip,headers=headers)
print(resp.status_code)
print(resp.text)
resp.close()
API是免费版本, 每分钟限制45次请求,使用POST请求方式, URL中的fields=58898是返回指定字段.
我在API的网站查询IP没有问题, 但是代码中一直返回空值, requests也没有报错, 所以现在有点不知道从哪里下手了, 请大佬们帮我分析一下, 或者给点思路! 跪谢!
回答:
import randomimport requests
import json
ip = [
"49.104.25.257",
"39.115.131.116"
]
api = "http://ip-api.com/batch?fields=58898&lang=zh_CN"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
"content-type": "application/json"
}
ip = json.dumps(ip, ensure_ascii=False, indent=2)
print(ip)
resp = requests.post(url=api, data=ip, headers=headers)
print(resp.status_code)
print(resp.text)
resp.close()
以上是 API返回空值的原因是什么? 的全部内容, 来源链接: utcz.com/p/939120.html