爬虫之 requests小结

文档
English – http://www.python-requests.org/en/master/
中文 – http://cn.python-requests.org/zh_CN/latest/
快速上手– http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
高级用法 – http://cn.python-requests.org/zh_CN/latest/user/advanced.html
源码
http://cn.python-requests.org/zh_CN/latest/user/install.html#id2

Requests 支持 Python 2.6—3.5,而且能在PyPy下完美运行。

import requests

r = requests.get(url) # r 是response的对象
r = requests.post(url, data = post_data)  # 提交表单 字典类型的表单数据
r = requests.get(url, headers=headers)  # headers 字典类型的请求头
r = requests.get(url, cookies=cookies)  # 带cookies的请求
r = requests.get("http://example.org", proxies=proxies) # 通过代理请求
proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}



r.text # 得到页面文本的内容 Unicode型的数据
r.encoding # 网页的编码 可以设置如 r.encoding = 'utf-8'
r.content # bytes型也就是二进制的数据, 想取图片,文件,则可以通过r.content
r.status_code  # 状态码
r.headers  # 服务器返回的响应头
r.cookies  # cookies
r.request.headers  # 发送到服务器的请求头

requests.get('http://github.com', timeout=0.001)  # timeout 设置超时时间
r.json() # 内置json解码器


s = requests.Session()  # 保持cookie  如保持登陆状态

具体用法 看文档

猜你喜欢

转载自blog.csdn.net/qq8677/article/details/72588766