简介

本文环境

Windows  10
Python   3.6.x
Requests 2.x

安装

pip install requests -i https://mirrors.aliyun.com/pypi/simple/
# socks5 支持
pip install requests[socks] -i https://mirrors.aliyun.com/pypi/simple/
proxies = {
    'http': 'socks5://127.0.0.1:1080',
    'https': 'socks5://127.0.0.1:1080'
}

技巧与示例

cookie

  • 添加 cookie
requests.utils.add_dict_to_cookiejar(cj, cookie_dict)
  • 如果 sn.headers['Cookie'] 有值,那么 sn.cookies 不再起作用

关闭 https 证书警告

import requests
from urllib3.exceptions import InsecureRequestWarning
# 关闭 https 证书警告
requests.urllib3.disable_warnings(InsecureRequestWarning)

高级用法

默认超时值

get url 25%

Requests 在 get 时 url 中的百分号(%)被强制编码为 25% 可用如下方法迂回解决:

import requests
sn = requests.Session()
url = 'http://xxx.net/xxx.aspx?Param=ASP.brief_result_aspx%23/%u5E74
req = requests.Request('GET', url)
prepped = sn.prepare_request(req)
prepped.url = prepped.url.replace('%25', '%')
r = sn.send(prepped)

或者拆分后重新拼接:

import requests
from urllib import parse
url = 'http://xxx.net/xxx.aspx?' + parse.urlencode(dict(parse.parse_qsl(parse.urlparse(url).query)))
r = sn.get(url)

POST 请求

  • POST 表单重复键值的处理
dic = {
        'key1': ['aaa', 'bbb', 'ccc'],
        'key2': 'xxx',
    }
r = requests.post(url, data=dic)

# 这样会被编码成:key1=aaa&key1=bbb&key1=ccc&key2=xxx

【code】

import requests
import json

r = requests.post('http://www.baidu.com', files={'key':'val'})
print('***test 1: %s' % r.request.body)
print('------------------------')
print(r.request.body.decode('utf8'))
print('\n')

line = json.dumps({'k1':'v1', 'k2':'v2'})
r = requests.post('http://www.baidu.com', files={'json':(None, line)})
print('***test 2: %s' % r.request.body)
print('------------------------')
print(r.request.body.decode('utf8'))

【output】

***test 1: b'--5b57f36c03ca462f93dfbd8dfc97e2d1\r\nContent-Disposition: form-data; name="key"; filename="key"\r\n\r\nval\r\n--5b57f36c03ca462f93dfbd8dfc97e2d1--\r\n'
------------------------
--5b57f36c03ca462f93dfbd8dfc97e2d1
Content-Disposition: form-data; name="key"; filename="key"

val
--5b57f36c03ca462f93dfbd8dfc97e2d1--


***test 2: b'--cf66a355e1f6441fa5a3079e1fafc43a\r\nContent-Disposition: form-data; name="json"\r\n\r\n{"k1": "v1", "k2": "v2"}\r\n--cf66a355e1f6441fa5a3079e1fafc43a--\r\n'
------------------------
--cf66a355e1f6441fa5a3079e1fafc43a
Content-Disposition: form-data; name="json"

{"k1": "v1", "k2": "v2"}
--cf66a355e1f6441fa5a3079e1fafc43a--
本文出自 qbit snap

qbit
268 声望279 粉丝