码迷,mamicode.com
首页 > 编程语言 > 详细

python requests学习

时间:2016-04-10 22:40:30      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

1、无参数直接get

>>> import requests
>>> r  = requests.get("http://httpbin.org/get")

>>> r.status_code
200
>>> print(r.text)
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.9.1"
  }, 
  "origin": "110.184.65.255", 
  "url": "http://httpbin.org/get"
}

 

同时支持json直接获取json解码后结果

>>> r.json()
{args: {}, origin: 110.184.65.255, url: http://httpbin.org/get, headers: {Accept: */*, Accept-Encoding: gzip, deflate, Host: httpbin.org, User-Agent: python-requests/2.9.1}}

 

2、有参数的get,制定params的参数的值。

>>> payload = {name:felix,password:12345678}
>>> r = requests.get(http://httpbin.org/get,params=payload)
>>> print(r.url)
http://httpbin.org/get?name=felix&password=12345678
>>> r.status_code
200
>>> print(r.text)
{
  "args": {
    "name": "felix", 
    "password": "12345678"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.9.1"
  }, 
  "origin": "110.184.65.255", 
  "url": "http://httpbin.org/get?name=felix&password=12345678"
}

可以用r.url打印生成的url.

3、修改HTTP头内容

>>> headers={user-agent:"IE9.0"}
>>> r = requests.get("http://httpbin.org/get",headers=headers)

 

4、POST方法, form格式

>>> payload
{name: felix, password: 12345678}
>>> r = requests.post("http://httpbin.org/post",data=payload)
>>> r.status_code
200
>>> print(r.text)
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "name": "felix", 
    "password": "12345678"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "28", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.9.1"
  }, 
  "json": null, 
  "origin": "110.184.65.255", 
  "url": "http://httpbin.org/post"
}

json格式

>>> r = requests.post("http://httpbin.org/post",json=payload,headers=headers)
>>> print(r.text)
{
  "args": {}, 
  "data": "{\"name\": \"felix\", \"password\": \"12345678\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "41", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "IE9.0"
  }, 
  "json": {
    "name": "felix", 
    "password": "12345678"
  }, 
  "origin": "110.184.65.255", 
  "url": "http://httpbin.org/post"
}

 

字符串格式:

r = requests.post(http://httpbin.org/post, data=json.dumps(payload))

 

5、超时

>>> r=requests.get("https://www.baidu.com",timeout=0.1)
>>> r=requests.get("https://www.baidu.com",timeout=0.005)
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\requests\packages\urllib3\connection.py", line 137, in _new_conn

Errors and Exceptions

In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise aConnectionError exception.

In the rare event of an invalid HTTP response, Requests will raise an HTTPError exception.

If a request times out, a Timeout exception is raised.

If a request exceeds the configured number of maximum redirections, a TooManyRedirects exception is raised.

All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.

 

更多信息详见:http://www.python-requests.org/en/master/user/quickstart/

 

python requests学习

标签:

原文地址:http://www.cnblogs.com/felixr/p/5376307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!