标签:基本使用 lib ase .json ble hub python-r tor 参数
requests库官方文档:https://2.python-requests.org/en/master/
coding:utf8
import requests
response = requests.get('http://www.httpbin.org/get')
print(response.text)
# coding:utf8
import requests
data = {
'name': 'Thanlon',
'age': 22,
'sex': '男'
}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)
# coding:utf8
import requests, json
response = requests.get('http://www.httpbin.org/get')
print(type(response.text))
# print(response.text)
print(response.json()) # 等价于json.loads(response.text)
print(type(response.json()))
# coding:utf8
import requests
response = requests.get('https://www.baidu.com/img/dong_5af13a1a6fd9fb2c587e68ca5038a3c8.gif')
print(type(response.text))
print(type(response.content))
print(response.text)
print(response.content) # 二进制流
# coding:utf8
import requests
response = requests.get('https://www.baidu.com/img/dong_5af13a1a6fd9fb2c587e68ca5038a3c8.gif')
with open('image.gif', 'wb') as f:
f.write(response.content)
f.close()
# coding:utf8
# get请求,添加headers
import requests
headers = {
'user-agent': 'Mouser-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36zilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
}
response = requests.get('https://www.zhihu.com/explore', headers=headers)
print(response.text)
#coding:utf8
#post请求,添加headers
import requests
data = {
'name': 'Thanlon',
'age': 22,
'sex': '男'
}
headers = {
'user-agent': 'Mouser-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36zilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
}
response = requests.post('http://httpbin.org/post', data=data, headers=headers)
print(response.text)
#coding:utf8
import requests
response = requests.get('http://httpbin.org')
print(type(response.status_code), response.status_code)#状态码 <class 'int'>
print(type(response.headers), response.headers)#响应头 <class 'requests.structures.CaseInsensitiveDict'>
print(type(response.cookies), response.cookies)#cookie <class 'requests.cookies.RequestsCookieJar'>
print(type(response.url), response.url)#请求的url <class 'str'>
print(type(response.history), response.history) # 访问的历史记录 <class 'list'>
#coding:utf8
import requests
response = requests.get('http://httpbin.org')
if not response.status_code == requests.codes.ok:#requests.codes.ok等价于200
pass
else:
print('Request Successfully')
#coding:utf8
import requests
files = {
'file': open('image.gif', 'rb') # file可自定义
}
response = requests.post('http://httpbin.org/post', files=files)
print(response.text)
#coding:utf8
import requests
response = requests.get('http://www.baidu.com')
print(response.cookies)
print(response.cookies.items()) # [('BDORZ', '27315')]
for key, value in response.cookies.items():
print(key + '=' + value)
#coding:utf8
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/BDORZ/123456')
response = s.get('http://httpbin.org/cookies')
print(response.text)
#coding:utf8
import requests
response = requests.get('https://www.12306.cn')
print(response.status_code)
#coding:utf8
import requests, urllib3
urllib3.disable_warnings() # 消除警报信息
response = requests.get('https://www.12306.cn', verify=False) # verify默认是True
print(response.status_code) # 没有进行证书验证,有警报信息,
#coding:utf8
import requests
response = requests.get('https://www.12306.cn', cert={'/path/server.crt', '/path/key'})
print(response.status_code)
#coding:utf8
import requests
proxies = {
'http': 'http://127.0.0.1:9743',
'https': 'https://127.0.0.1:9743'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
#coding:utf8
import requests
proxies = {
'http': 'http://user:password@127.0.0.1:9743',
'https': 'https://user:password@127.0.0.1:9743'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
import requests
proxies = {
'http': 'socks5://127.0.0.1:9743',
'https': 'socks5://127.0.0.1:9743'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
#coding:utf8
import requests
response = requests.get('http://httpbin.org', timeout=1)
print(response.status_code)
遇到401错误,即:请求被禁止,需要加上auth参数
#coding:utf8
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
#response = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print(response.status_code)
#coding:utf8
import requests
from requests.exceptions import ReadTimeout, HTTPError, RequestException, ConnectionError
try:
response = requests.get('http://httpbin.org', timeout=0.3)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except HTTPError:
print('Http Error')
# except ConnectionError:
# print('Connection Error')
except RequestException:
print('Request Error ')
标签:基本使用 lib ase .json ble hub python-r tor 参数
原文地址:https://www.cnblogs.com/qikeyishu/p/10752774.html