码迷,mamicode.com
首页 > 编程语言 > 详细

python请求库

时间:2020-03-01 10:59:52      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:from   rac   ack   ssid   html   port   bin   请求   war   

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:password@xxxxx: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=('用户名', '密码'))

python请求库

标签:from   rac   ack   ssid   html   port   bin   请求   war   

原文地址:https://www.cnblogs.com/codemeta-2020/p/12388376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!