python请求库

urllib库

python原生基本库

requests库

安装 pip install requests

它是在urllib3基础上更加强大的请求库

实例引入

import requests
response = requests.get("https://www.baidu.com")
print(response.status_code) 
print(response.text)   
print(response.content) 
print(response.cookies)
print(response.url)

各种请求方式

import requests

requests.post("http://httpbin.org/post")
requests.put("http://httpbin.org/put")
requests.head("http://httpbin.org/get")
requests.delete("http://httpbin.org/delete")
requests.options("http://httpbin.org/get")

带参数的get请求

import requests
response = requests.get("http://httpbin.org/get")
print(response.text)
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.21.0", 
    "X-Amzn-Trace-Id": "Root=1-5e5a5e25-0029a7ce338de2665befc666"
  }, 
  "origin": "183.230.193.190", 
  "url": "http://httpbin.org/get"
}

?

import requests

data = {
    "name":"germey",
    "age":"22"
}
response = requests.get("http://httpbin.org", params = data)  # ==> http://httpbin.org/?name=germey&age=22
print(response.url)
print(response.text)

添加headers

import requests

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
response = requests.get("https://www.bilibili.com", headers=headers)
print(response.text)

基本post请求

import requests

data = {
    "name":"germey",
    "age":"22"
}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
response = requests.post("https://httpbin.org/post", data = data, headers = headers)
print(response.url)
print(response.text)
https://httpbin.org/post
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "22", 
    "name": "germey"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "18", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-5e5a6344-f03769b7e0c3043b3f759a1a"
  }, 
  "json": null, 
  "origin": "183.230.181.118", 
  "url": "https://httpbin.org/post"
}

?

获取cookie

import requests

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
response = requests.get("http://www.baidu.com", headers=headers)
print(response.cookies)
for key, value in response.cookies.items():
    print(key + "=" + value)
<RequestsCookieJar[<Cookie H_PS_PSSID=1444_21125_30839_30823_26350_30717 for .baidu.com/>, <Cookie BDSVRTM=0 for www.baidu.com/>, <Cookie BD_HOME=1 for www.baidu.com/>]>
H_PS_PSSID=1444_21125_30839_30823_26350_30717
BDSVRTM=0
BD_HOME=1

会话维持

模拟登录

import requests

requests.get("http://httpbin.org/cookies/set/number/12345678")
response = requests.get("http://httpbin.org/cookies")
print(response.text)
{
  "cookies": {}
}

?

这相当于用不同的浏览器请求,无法获取和维持会话

import requests

s = requests.Session()
s.get("http://httpbin.org/cookies/set/number/12345678")
response = s.get("http://httpbin.org/cookies")
print(response.text)
{
  "cookies": {
    "number": "12345678"
  }
}

?

证书验证

import requests
from requests.packages import urllib3
urllib3.disable_warnings()
requests.get("https://www.bilibili.com", verify=False)
<Response [200]>

代理设置

import requests
proxies = {
    "http":"http://xxx.xxx:port"
    "https":"https://xxx.xx:port"
    "https":"https://user::port"
}
response = requests.get("https://xxxxxxx", proxies = proxies)

超时设置

import requests
requests.get("http://httpbin.org/get", timeout=1)
<Response [200]>

登录认证设置

import requests
from requests.auth import HTTPBasicAuth
requests.get("url", auth=('用户名', '密码'))