一、示例:向百度发送get请求(前提已经安装了requests包):
import requests baiduRespones = requests.get("http://www.baidu.com") print(baiduRespones.text)
注意:响应结果中文会出现乱码的处理方法,修改编码格式,例如:
baiduRespones.encoding = "utf-8"
二、get请求中添加参数
# (1)传递字符串 baiduRespones = requests.get("http://www.baidu.com?name=testname") baiduRespones1 = requests.get(url="http://www.baidu.com", params="name=testname")
# (2)传递字典 dict = { "name": "testname" } baiduRespones2 = requests.get(url="http://www.baidu.com", params=dict)
三、post请求中携带参数(参数为表单数据和json数据)
# 导入requests包 import requests # 发送post请求 dataValue = { "head": { "code": 200, "message": "" }, "body": { "parameters": { "username": "test", "password": "123456", "checkVerificate": "false", "verificateCacheKey": "", "verificate": "" }, "datastores": {} } } postResponse = requests.post("http://192.168.1.79:8088/checkLogin", data=dataValue) print(postResponse.json)
如果传递的参数是json数据,那么应该写成:
postResponse = requests.post("http://192.168.1.79:8088/checkLogin", json=dataValue)