requests库

1、安装

pip install requests

2、发送请求

常用的有requests.get()、requests.post()、requests.request()

#发送get请求
url = "http://httpbin.org/get" response = requests.get(url)
#发送post请求
response = requests.post(url)

结果:Method Not Allowed:请求方法不被允许,url中指定了请求方法为get,而实际发送的是post请求

requests.request("get",url)

3、获取响应

response.text:获取响应体数据,数据类型是字符串,response.status_code获取状态码,response.content()获取二进制的文本数据,response.json()获取json,数据类型是字典。

print(response.text)#字符串
# #获取状态码
print(response.status_code)
#
# #获取content二进制的数据
print(response.content)
print(response.json())

 

4、传参

4.1查询字符串

发送get/post的url参数,直接在url中传参,?name=zhangsan&age=30

import requests
url = f"http://httpbin.org/get?name=zhangsan&age=30" response = requests.get(url) print(response.text)

#响应结果

{
  "args": {
  "age": "30",
  "name": "zhangsan"
  },
  "headers": {
   "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
   "Host": "httpbin.org",
    "User-Agent": "python-requests/2.24.0",
    "X-Amzn-Trace-Id": "Root=1-6335a5a7-67f798d02d9374252c6198b1"
  },
  "origin": "117.172.87.190",
  "url": "http://httpbin.org/get?name=zhangsan&age=30"
}

#发送get/post请求的params传递参数
import requests
url= "http://httpbin.org/get" data = { "name":"zhangsan", "age":30 } resp = requests.get(url,params = data) print(resp.text)

#响应结果

{
"args": {
"age": "30",
"name": "zhangsan"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6335a6fd-23a026e41eadbffc779f1192"
},
"origin": "117.172.87.190",
"url": "http://httpbin.org/get?name=zhangsan&age=30"
}

form-data

import requests
url= "http://httpbin.org/post" params = {"name":"zhangsan","age":30} data ={"sex":""} resp = requests.post(params=params,data=data,url=url) print(resp.text)


#响应结果

{
"args": {
"age": "30",
"name": "zhangsan"
},
"data": "",
"files": {},
"form": {   #form-data
"sex": "\u7537"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "13",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6335a812-4aa53f4c2e61acd97574bd1a"
},
"json": null,
"origin": "117.172.87.190",
"url": "http://httpbin.org/post?name=zhangsan&age=30"
}

json

import requests
url= "http://httpbin.org/post"

params = {"name":"zhangsan","age":30}

data ={"sex":""}

resp = requests.post(params=params,json=data,url=url)
print(resp.text)

 

响应结果

{
"args": {
"age": "30",
"name": "zhangsan"
},
"data": "{\"sex\": \"\\u7537\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "17",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6335a8a2-3d111458135b2d2440229cf2"
},
"json": {
"sex": "\u7537"
},
"origin": "117.172.87.190",
"url": "http://httpbin.org/post?name=zhangsan&age=30"
}

 

posted @ 2022-09-29 22:19  小疯狗  阅读(39)  评论(0编辑  收藏  举报