标签:from rac ack ssid html port bin 请求 war
python原生基本库
安装
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")
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)
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)
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"
}
?
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=('用户名', '密码'))
标签:from rac ack ssid html port bin 请求 war
原文地址:https://www.cnblogs.com/codemeta-2020/p/12388376.html