Python Requests库详解

Requests是用Python语言编写,基于urllib,采用Apache2Licensed开源协议的HTTP库。
它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。

一句话--Python实现的简单易用的HTTP库

安装Requests

pip3 isntall requests

request

实例引入

import requests
response = requests.get('https://www.baidu.com/')
print(type(response))
print(response.status_code)
print(type(response.text))
print(response.text)
print(response.cookies)

各种请求方式

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

请求

基本GET请求

基本写法
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
带参数GET请求
import requests
response = requests.get('http://httpbin.org?name=puqunzhu&age=23')
print(response.text)
import requests
data = {
    'name':'puqunzhu',
    'age':22
}
response = requests.get("http://httpbin/get",params=data)
print(response.text)
解析json
import requests
response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
print(type(response.json()))
获取二进制数据
import requests
response = requests.get("https://github.com/favicon.ico")
print(type(response.text),type(response.content))
print(response.text)
print(response.content)
import requests
response = requests.get("https://github.com/favicon.ico")
with open('favicon.ico','wb')as f:
    f.write(response.content)
    f.close()
添加headers
import requests
response = requests.get("https://www.zhihu.com/explore")
print(response.text)
import requests
headers={
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
response = requests.get("https://www.zhihu.com/explore",headers=headers)
print(response.text)

基本POST请求

import requests
data = {'name':'puqunzhu','age':'22'}
response = requests.post("http://httpbin.org/post",data=data)
print(response.text)
import requests
data = {'name':'puqunzhu','age':22}
headers={
  'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'  
}
response = requests.post("http://httpbin.org/post",data=data,headers=headers)
print(response.json())

响应

response属性
import requests
response = requests.get('http://www.janshu.com')
print(type(response.status_code),response.status_code)
print(type(response.headers),response.headers)
print(type(response.cookies),response.cookies)
print(type(response.url),response.url)
print(type(response.history),response.history)
状态码判断
import requests
response = requests.get('http://www.jianshu.com')
exit()if not response.status_code == requests.codes.ok else print('Request Successfully')
import requests
response = requests.get("http://www.jianshu.com")
exit()if not response.status_code == 200 else print('Request Successfully')

文本操作

文件上传
import requests
files ={'file':open('favicon.ico','rb')}
response = requests.post("http://httpbin.org/post",files=files)
print(response.text)
获取cookie
import requests
response =requests.get("https://www.baidu.com")
print(response.cookies)
for key,value in response.cookies.items():
    print(key+'='+value)
会话维持
模拟登录
import requests
requests.get("http://httpbin.org/cookies/set/number/123456789")
response = requests.get("http://httpbin.org/cookies")
print(response.text)
import requests
s =  requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
证书验证
import requests
response = requests.get('https://www.12306.cn')
print(response.status_code)
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
import requests
response = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))
print(response.status_code)
代理设置
import requests
proxies = {
    'http':'http://61.135.217.7:80',
    'https':'https://61.150.96.27:46111',
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
import requests
proxies = {
    'http':'http://user:password@61.135.217.7:80/',
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
pip3 install 'requests{socks}'
import requests
proxies = {
    'http':'socks5://61.135.217.7:80',
    'https':'socks5://61.150.96.27:46111',
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
超时设置
import requests
try:
    response = requests.get("http://httpbin.org/get",timeout = 1)
    print(response.status_code)
except ReadTimeout:
    print('Timeout')
认证设置
import requests
from requests.auth import HTTPBasicAuth
r = requests.get("http://120.27.34.24:9001",auth=HTTPBasicAuth('user','123'))
print(r.status_code)
异常处理
import requests
from requests.exceptions import ReadTimeout,HTTPError,RequestException
try:
    response = requests.get('http://httpbin.org/get',timeout = 0.01)
    print(response.status_code)
except ReadTimeout:
    print('Timeout')
except HTTPError:
    print('Http error')
except RequestException:
    print('Error')
posted @ 2018-10-17 16:51  蒲群柱  阅读(282)  评论(0编辑  收藏  举报