update python_requests.py

master
xianhu 2016-11-10 18:18:44 +08:00
parent 6179eada84
commit d7e070307a
1 changed files with 85 additions and 70 deletions

View File

@ -6,48 +6,59 @@ python_requests.py by xianhu
import requests
# 尝试获取某个网页, 这里的r是一个Response对象
r = requests.get("https://github.com/timeline.json")
print(type(r))
# Requests简便的API意味着所有HTTP请求类型都是显而易见的
# 不同方式获取网页内容, 返回一个Response对象, 请求的参数可以为url或Request对象
r0 = requests.get("https://github.com/timeline.json")
r1 = requests.post("http://httpbin.org/post")
r2 = requests.put("http://httpbin.org/put")
r3 = requests.delete("http://httpbin.org/delete")
r4 = requests.head("http://httpbin.org/get")
r5 = requests.options("http://httpbin.org/get")
r6 = requests.patch("http://httpbin.org/get")
# 传递URL参数: 字典
payload = {"key1": "value1", "key2": "value2"}
r = requests.get("http://httpbin.org/get", params=payload)
# Request对象:
# class requests.Request(method=None, url=None, headers=None, files=None, data=None, params=None, auth=None, cookies=None, hooks=None, json=None)
# 上边所有的获取方式都调用底层的request方法, 所以request方法有的参数, 上边几个函数都应该有:
# requests.request(method, url, **kwargs)
# kwargs包括: params / data / json / headers / cookies / files / auth / timeout / allow_redirects(bool) / proxies / verify(bool) / stream / cert
# Response对象: class requests.Response
# 包含的主要属性: content / cookies / encoding / headers / history / is_permanent_redirect / is_redirect / reason / status_code / text / url 等
# 包含的主要方法: iter_content(chunk_size=1, decode_unicode=False) / iter_lines(chunk_size=512, decode_unicode=None, delimiter=None)
# 包含的主要方法: close() / json(**kwargs) / raise_for_status() 等
# 以字典的形式传递URL参数, 也可以直接以?xx=xx&xx=xx的形式将其放在url后
params = {"key1": "value1", "key2": "value2"}
r = requests.get("http://httpbin.org/get", params=params)
print(r.url) # http://httpbin.org/get?key2=value2&key1=value1
# 传递URL参数: 字典里带有列表
payload = {"key1": "value1", "key2": ["value2", "value3"]}
r = requests.get("http://httpbin.org/get", params=payload)
# 以字典的形式传递URL参数: 字典里带有列表
params = {"key1": "value1", "key2": ["value2", "value3"]}
r = requests.get("http://httpbin.org/get", params=params)
print(r.url) # http://httpbin.org/get?key1=value1&key2=value2&key2=value3
# 获取正常内容
# 获取网页内容
r = requests.get("https://github.com/timeline.json")
print(r.text) # "[{"repository":{"open_issues":0,"url":"https://github.com/...
print(r.encoding) # "utf-8"
print(r.text) # 返回正常的网页内容, 即解压解码之后的内容
print(r.content) # 返回byte类型的网页内容, 即值解压, 没有解码
print(r.json()) # 如果网页内容为json, 直接返回一个json对象
print(r.encoding) # 返回网页的编码: "utf-8"
# Requests会自动解码来自服务器的内容(基于HTTP头部对响应的编码作出有根据的推测), 或者你自己更改
# Requests会自动解码来自服务器的内容, 也可以自己更改
r.encoding = "ISO-8859-1"
print(r.text) # 此时使用新的r.encoding新值
print(r.text) # 此时使用新的r.encoding解码后的新值
# 二进制响应内容, Requests会自动为你解码 gzip 和 deflate 传输编码的响应数据
print(r.content) # b"[{"repository":{"open_issues":0,"url":"https://github.com/...
# JSON 响应内容, Requests中也有一个内置的JSON解码器
print(r.json()) # [{u"repository": {u"open_issues": 0, u"url": "https://github.com/...
# 编码的其他操作
# requests.utils.get_encodings_from_content(content): Returns encodings from given content string.
# requests.utils.get_encoding_from_headers(headers): Returns encodings from given HTTP Header Dict.
# requests.utils.get_unicode_from_response(r): Returns the requested content back in unicode.
# 原始响应内容: 获取来自服务器的原始套接字响应
r = requests.get("https://github.com/timeline.json", stream=True)
print(r.raw) # <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
print(r.raw.read(10)) # "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03"
# 一般情况下, 应该以下面的模式将文本流保存到文件
# 一般情况下, 应该以下面的模式将文本流保存到文件
with open("test", "wb") as fd:
for chunk in r.iter_content(chunk_size=256):
fd.write(chunk)
@ -56,44 +67,8 @@ with open("test", "wb") as fd:
# 定制请求头: 一个字典
headers = {"user-agent": "my-app/0.0.1"}
r = requests.get("https://api.github.com/some/endpoint", headers=headers)
print(r.request.headers) # 获取该请求的头部
# 更加复杂的POST请求: 表单
payload = {"key1": "value1", "key2": "value2"}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text)
# POST一个多部分编码(Multipart-Encoded)的文件
files = {"file": open("report.xls", "rb")}
r = requests.post("http://httpbin.org/post", files=files)
print(r.text)
# 你可以显式地设置文件名, 文件类型和请求头
files = {"file": ("report.xls", open("report.xls", "rb"), "application/vnd.ms-excel", {"Expires": "0"})}
r = requests.post("http://httpbin.org/post", files=files)
print(r.text)
# 你也可以发送作为文件来接收的字符串
files = {"file": ("report.csv", "some,data,to,send\nanother,row,to,send\n")}
r = requests.post("http://httpbin.org/post", files=files)
print(r.text)
# 响应状态码
r = requests.get("http://httpbin.org/get")
print(r.status_code) # 200
print(r.status_code == requests.codes.ok) # True 响应状态码查询
# 如果发送了一个错误请求(4XX客户端错误, 或5XX服务器错误响应), 可以通过 Response.raise_for_status() 来抛出异常:
bad_r = requests.get("http://httpbin.org/status/404")
print(bad_r.status_code) # 404
bad_r.raise_for_status()
# Traceback (most recent call last):
# File "requests/models.py", line 832, in raise_for_status
# raise http_error
# requests.exceptions.HTTPError: 404 Client Error
# 响应头, 一个Python字典形式展示的服务器响应头, HTTP头部是大小写不敏感的
print(r.headers)
print(r.request.headers) # 获取request的头部
print(r.headers) # 获取response的头部
# {
# "content-encoding": "gzip",
# "transfer-encoding": "chunked",
@ -106,15 +81,53 @@ print(r.headers)
print(r.headers["Content-Type"]) # "application/json"
print(r.headers.get("content-type")) # "application/json"
# Cookie: 如果某个响应中包含一些 cookie
# 更加复杂的POST请求: 表单
post_dict = {"key1": "value1", "key2": "value2"}
r = requests.post("http://httpbin.org/post", data=post_dict)
print(r.text)
# POST一个多部分编码(Multipart-Encoded)的文件
files = {"file": open("report.xls", "rb")}
r = requests.post("http://httpbin.org/post", files=files)
print(r.text)
# 你可以显式地设置文件名, 文件类型和请求头
files = {"file": ("report.xls", open("report.xls", "rb"), "application/vnd.ms-excel", {"Expires": "0"})}
r = requests.post("http://httpbin.org/post", files=files)
print(r.text)
# 你也可以发送文本字符串
files = {"file": ("report.csv", "some,data,to,send\nanother,row,to,send\n")}
r = requests.post("http://httpbin.org/post", files=files)
print(r.text)
# 响应状态码
r = requests.get("http://httpbin.org/get")
print(r.status_code) # 200
print(r.status_code == requests.codes.ok) # True 响应状态码查询
# 如果发送了一个错误请求(4XX客户端错误, 或5XX服务器错误响应), 可以通过 Response.raise_for_status() 来抛出异常:
bad_r = requests.get("http://httpbin.org/status/404")
print(bad_r.status_code) # 404
bad_r.raise_for_status() # 引发异常
# Cookie: 如果某个响应中包含一些cookie, 则会被放到response.cookies(CookieJar类型)中
r = requests.get("http://example.com/some/cookie/setting/url")
print(r.cookies["example_cookie_name"]) # "example_cookie_value"
# 要想发送你的cookies到服务器, 可以使用cookies参数, 一个字典
# 要想发送你的cookies到服务器, 可以使用cookies参数(一个字典)
cookies = dict(cookies_are="working")
r = requests.get("http://httpbin.org/cookies", cookies=cookies)
print(r.text)
# cookie的其他操作
# requests.utils.dict_from_cookiejar(cj): Returns a key/value dictionary from a CookieJar.
# requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True): Returns a CookieJar from a key/value dictionary.
# requests.utils.add_dict_to_cookiejar(cj, cookie_dict): Returns a CookieJar from a key/value dictionary.
# 通用CookieJar类, 一个cookielib.CookieJar, 但是提供一个dict接口
# class requests.cookies.RequestsCookieJar(policy=None): Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.
# 会话对象: 会话对象让你能够跨请求保持某些参数, 它也会在同一个Session实例发出的所有请求之间保持cookie
s = requests.Session()
s.get("http://httpbin.org/cookies/set/sessioncookie/123456789")
@ -122,6 +135,9 @@ s.get("http://httpbin.org/cookies")
for cookie in s.cookies:
print(cookie)
# 如果你要手动为会话添加cookie, 就是用Cookie utility函数来操纵Session.cookies
requests.utils.add_dict_to_cookiejar(s.cookies, {"cookie_key": "cookie_value"})
# 会话也可用来为请求方法提供缺省数据, 这是通过为会话对象的属性提供数据来实现的
s.auth = ("user", "pass")
s.headers.update({"x-test": "true"})
@ -131,31 +147,20 @@ s.get("http://httpbin.org/headers", headers={"x-test2": "true"}) # both "
s.get("http://httpbin.org/cookies", cookies={"from-my": "browser"}) # 带有cookie
s.get("http://httpbin.org/cookies") # 不带cookie
# 如果你要手动为会话添加cookie, 就是用Cookie utility函数来操纵Session.cookies
requests.utils.add_dict_to_cookiejar(s.cookies, {"cookie_key": "cookie_value"})
for cookie in s.cookies:
print(cookie)
# 会话还可以用作前后文管理器
with requests.Session() as s:
s.get("http://httpbin.org/cookies/set/sessioncookie/123456789")
# class requests.Session类, 和requests外层有的函数/属性基本一致, 只不过是封装了一层跨域请求的功能
# 重定向与请求历史, 默认情况下, 除了HEAD, Requests会自动处理所有重定向
# 可以使用响应对象的history方法来追踪重定向
# Response.history 是一个 Response 对象的列表, 为了完成请求而创建了这些对象. 这个对象列表按照从最老到最近的请求进行排序
r = requests.get("http://github.com")
# 重定向与请求历史, 默认情况下, 除了HEAD, Requests会自动处理所有重定向, 可以通过allow_redirects参数禁用重定向处理
# 可以使用响应对象的history方法来追踪重定向, Response.history 是一个Response对象的列表, 按照从最老到最近的请求进行排序
r = requests.get("http://github.com", allow_redirects=True)
print(r.status_code) # 200
print(r.history) # [<Response [301]>]
# 如果你使用的是GET、OPTIONS、POST、PUT、PATCH 或者 DELETE, 那么你可以通过 allow_redirects 参数禁用重定向处理
r = requests.get("http://github.com", allow_redirects=False)
print(r.status_code) # 301
print(r.history) # []
# 如果你使用了HEAD, 你也可以启用重定向
r = requests.head("http://github.com", allow_redirects=True)
print(r.history) # [<Response [301]>]
# 超时, 设置timeout参数
requests.get("http://github.com", timeout=0.001)
# Traceback (most recent call last):
@ -166,8 +171,8 @@ requests.get("http://github.com", timeout=0.001)
# timeout并不是整个下载响应的时间限制, 而是如果服务器在timeout秒内没有应答, 将会引发一个异常
# 更精确地说, 是在timeout秒内没有从基础套接字上接收到任何字节的数据时
requests.get("https://github.com", timeout=5)
# 这一 timeout 值将会用作 connect 和 read 二者的 timeout
# 如果要分别制定, 就传入一个元组
# 上边的timeout值将会用作 connect 和 read 二者的timeout, 如果要分别制定, 就传入一个元组
requests.get("https://github.com", timeout=(3.05, 27))
# 错误与异常: 遇到网络问题(如: DNS 查询失败、拒绝连接等)时, Requests 会抛出一个 ConnectionError 异常
@ -176,6 +181,16 @@ requests.get("https://github.com", timeout=(3.05, 27))
# 若请求超过了设定的最大重定向次数, 则会抛出一个 TooManyRedirects 异常
# 所有Requests显式抛出的异常都继承自 requests.exceptions.RequestException
# 所有异常
# exception requests.RequestException(*args, **kwargs): There was an ambiguous exception that occurred while handling your request.
# exception requests.ConnectionError(*args, **kwargs): A Connection error occurred.
# exception requests.HTTPError(*args, **kwargs): An HTTP error occurred.
# exception requests.URLRequired(*args, **kwargs): A valid URL is required to make a request.
# exception requests.TooManyRedirects(*args, **kwargs): Too many redirects.
# exception requests.ConnectTimeout(*args, **kwargs): The request timed out while trying to connect to the remote server.
# exception requests.ReadTimeout(*args, **kwargs): The server did not send any data in the allotted amount of time.
# exception requests.Timeout(*args, **kwargs): The request timed out.
# SSL证书验证, verify设置为True表示检查证书, 设置为False表示忽略证书
requests.get("https://kennethreitz.com", verify=True) # 未设置SSL证书, 抛出异常
# requests.exceptions.SSLError: hostname "kennethreitz.com" doesn"t match either of "*.herokuapp.com", "herokuapp.com"
@ -217,4 +232,4 @@ proxies = {
requests.get("http://example.org", proxies=proxies)
# 关闭InsecurePlatformWarning
requests.packages.urllib3.disable_warnings()
# requests.packages.urllib3.disable_warnings()