需求:根据接口文档请求数据
1:假设接口文档内容如下
接口地址:http://open.api.....
请求方式:post
请求参数说明:
token:050dbeb9d48b9f12fdf17e677d078a..
src:br
product:项目名
company:公司名
2:发送请求
import requests
def deal(project_name, company_name):
try:
# 请求网址
url = "http://open.api....."
# 请求头
headers = {"Content-Type": "application/x-www-form-urlencoded"}
# 参数
para = {"token": "050dbeb9d48b9f12fdf17e677d078a..", "src": "br",
"product": '{name}'.format(name=project_name), "company": '{company}'.format(company=company_name)}
r = requests.post(url, data=para, headers=headers)
print("获取返回的状态码", r.status_code)
json_r = r.json()
print("json类型转化成python数据类型", json_r)
except BaseException as e:
print("请求失败!", str(e))
if __name__ == '__main__':
deal('游仁', '北京游仁商贸有限公司')