好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

pythonrequests快速入门介绍

这篇文章主要介绍了python requests 使用快速入门教程,使用 Requests 发送网络请求非常简单,具体操作方法,大家参考下本文吧

>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options(http://httpbin.org/get) 
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload) 
>>> print(r.url)
http://httpbin.org/get?key2=value2&key1=value1 
>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
>>> r = requests.get('http://httpbin.org/get', params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3 
>>> import requests
>>> r = requests.get('https://github测试数据/timeline.json')
>>> r.text
u'[{"repository":{"open_issues":0,"url":"https://github测试数据/... 
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1' 
>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github测试数据/... 
>>> from PIL import Image
>>> from io import BytesIO
>>> i = Image.open(BytesIO(r.content)) 
>>> import requests
>>> r = requests.get('https://github测试数据/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github测试数据/... 
>>> r = requests.get('https://github测试数据/timeline.json', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03' 
with open(filename, 'wb') as fd:
 for chunk in r.iter_content(chunk_size):
  fd.write(chunk) 
>>> jar = requests.cookies.RequestsCookieJar()
>>> jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
>>> jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
>>> url = 'http://httpbin.org/cookies'
>>> r = requests.get(url, cookies=jar)
>>> r.text
'{"cookies": {"tasty_cookie": "yum"}}' 
>>> r = requests.get('http://github测试数据')
>>> r.url
'https://github测试数据/'
>>> r.status_code
200
>>> r.history
[<Response [301]>] 
>>> r = requests.get('http://github测试数据', allow_redirects=False)
>>> r.status_code
301
>>> r.history
[] 
>>> r = requests.head('http://github测试数据', allow_redirects=True)
>>> r.url
'https://github测试数据/'
>>> r.history
[<Response [301]>] 
>>> requests.get('http://github测试数据', timeout=0.001)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github测试数据', port=80): Request timed out. (timeout=0.001) 

注意

timeout 仅对连接过程有效,与响应体的下载无关。 timeout 并不是整个下载响应的时间限制,而是如果服务器在 timeout 秒内没有应答,将会引发一个异常(更精确地说,是在timeout 秒内没有从基础套接字上接收到任何字节的数据时)If no timeout is specified explicitly, requests do not time out.

错误与异常

遇到网络问题(如:DNS 查询失败、拒绝连接等)时,Requests 会抛出一个 ConnectionError 异常。

如果 HTTP 请求返回了不成功的状态码, Response.raise_for_status() 会抛出一个 HTTPError 异常。

若请求超时,则抛出一个 Timeout 异常。

若请求超过了设定的最大重定向次数,则会抛出一个 TooManyRedirects 异常。

所有Requests显式抛出的异常都继承自 requests.exceptions.RequestException 。

以上就是python requests快速入门介绍的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于pythonrequests快速入门介绍的详细内容...

  阅读:36次