python requests

快速上手http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的 完成浏览器可有的任何操作。

http://httpbin.org/  httpbin这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。

安装:pip install requests

1、Get请求

# 1、无参数实例
import requests

ret = requests.get('https://www.baidu.com', timeout=10)

print ret.url
print ret.text


# 2、有参数实例
import requests

payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("https://www.baidu.com", params=payload)

print ret.url
print ret.text

2、Post请求

# 1、基本POST实例
import requests

payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://www.baidu.com", data=payload)

print ret.text


# 2、发送请求头和数据实例

import requests
import json

url = 'http://www.baidu.com'
payload = {'key': 'value'}
headers = {'content-type': 'application/json'}

ret = requests.post(url, data=json.dumps(payload), headers=headers)

print ret.text
print ret.cookies
print ret.headers
原文地址:https://www.cnblogs.com/shhnwangjian/p/5726205.html