python3 requests详解_Python3 Requests库详解(学习笔记)

使用requests的相关方法操作

import requests

response = requests.get('https://www.baidu.com')

print(type(response))

print(response.status_code)

print(type(response.text))

print(response.cookies)

各种请求方式

带参数的GET请求

import requests

print(response.text)

构造GET请求参数

import requests

data = {

'name':'gg',

'age':18

}

response = requests.get('http://httpbin.org/get",params = data)

print(response.text)

解析JSON

import requests

import json

response = requests.get('http://httpbin.org/get')

print(type(response.text))

print(response.json())

print(type(response.json()))

获取二进制数据并写入文件

import requests

with open('d:\\favicon.ico','wb') as f:

f.write(response.content)

f.close()

带有headers的POST请求

import requests

headers = {

'User-Agent':'Ruby'

}

data = {'name':'gg','age':'18'}

response = requests.post('http://httpbin.org/post',data=data,headers=headers)

print(response.text)

响应部分

response 属性

response.status_code

response.headers

response.cookies

response.url

response.history

requests 的高级操作

文件上传

import reqeusts

files = {'file':open('d:\\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)

会话维持 模拟登陆

使用session对象

import requests

s = requests.Session()

s.get('http://httpbin.org/cookies/set/number/123456789')

response = s.get('http://httpbin.org/cookies')

print(response.text)

证书验证

requests打开https的时候,判断证书是否是合法的,如果证书是非法的,那么我们使用常规的方法是无法访问的

使用证书 使用指定证书和密钥

import requests

response = requests.get('https://123456789.com,cert=('/path/server.crt','/path/key'))

print(requests.status_code)

有关于代理的使用

单一代理设置

import requests

proxies = {

}

response = requests.get('https://www.google.com',proxies=proxies)

print(response.status_code)

带用户名密码的代理设置

import requests

proxies = {

'http':'http://username:[email protected]:9991'

}

response = requests.get('https://www.google.com',proxies=proxies)

print(response.status_code)

使用Socks代理

pip install requests[socks]

import requests

proxies = {

'http':'socks5://127.0.0.1:9992',

'https':'socks5://127.0.0.1:9992'

}

response = requests.get('https://www.google.com',proxies=proxies)

print(response.status_code)

认证设置

对于认证页面,需要使用认证方式

设置认证值

import requests

from requests.auth import HTTPDigestAuth

r = requests.get('http://192.168.16.1',auth=HTTPDigestAuth('123456','123456'))

print(r.status_code)

异常处理

import requests

from requests.exceptions import ReadTimeout,HTTPError,RequestException

try:

response = requests.get('http://httpbin.org/get',timeout = 0.1)

print(response.status_code)

except ReadTimeout:

print('Time Out!!!')

except HTTPError:

print('HTTP Error!!!')

except RequestException:

print('Error!!!')

你可能感兴趣的:(python3,requests详解)