python requests 应用

时间:2021-09-16 16:14:27   收藏:0   阅读:58

1. GET 请求

import requests

url = "http://xxxx"
body = {"name": "zhangsan"}
headers = {"Content-Type": "application/json;charset=utf-8"}

# params 字典类型自动转url编码
response = requests.get(url = url, params=body, headers = headers)

# 以unicode格式返回响应的内容
print(response.text)

# 返回响应码
print(response.status_code)

# 返回字典数据
print(response.json())

2. POST 请求

response = requests.post(url = url, data = json.dumps(body), headers = headers)

3. url 转码,POST 请求

from urllib import parse

headers["content_type"] = "application/x-www-form-urlencoded;charset=utf-8"

response = requests.post(url=url, data = parse.urlencode(body), headers=headers)

原文:https://www.cnblogs.com/test-works/p/15268980.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!