牛骨文教育服务平台(让学习变的简单)

作者:1world0x00(说明:在入选本教程的时候,我进行了适当从新编辑)

requests是一个用于在程序中进行http协议下的get和post请求的库。

安装

easy_install requests

或者用

pip install requests

安装好之后,在交互模式下运行:

>>> import requests
>>> dir(requests)
["ConnectionError", "HTTPError", "NullHandler", "PreparedRequest", "Request", "RequestException", "Response", "Session", "Timeout", "TooManyRedirects", "URLRequired", "__author__", "__build__", "__builtins__", "__copyright__", "__doc__", "__file__", "__license__", "__name__", "__package__", "__path__", "__title__", "__version__", "adapters", "api", "auth", "certs", "codes", "compat", "cookies", "delete", "exceptions", "get", "head", "hooks", "logging", "models", "options", "packages", "patch", "post", "put", "request", "session", "sessions", "status_codes", "structures", "utils"]

从上面的列表中可以看出,在http中常用到的get,cookies,post等都赫然在目。

get请求

>>> r = requests.get("http://www.itdiffer.com")

得到一个请求的实例,然后:

>>> r.cookies
<<class "requests.cookies.RequestsCookieJar">[]>

这个网站对客户端没有写任何cookies内容。换一个看看:

>>> r = requests.get("http://www.1world0x00.com")
>>> r.cookies
<<class "requests.cookies.RequestsCookieJar">[Cookie(version=0, name="PHPSESSID", value="buqj70k7f9rrg51emsvatveda2", port=None, port_specified=False, domain="www.1world0x00.com", domain_specified=False, domain_initial_dot=False, path="/", path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>

原来这样呀。继续,还有别的属性可以看看。

>>> r.headers
{"x-powered-by": "PHP/5.3.3", "transfer-encoding": "chunked", "set-cookie": "PHPSESSID=buqj70k7f9rrg51emsvatveda2; path=/", "expires": "Thu, 19 Nov 1981 08:52:00 GMT", "keep-alive": "timeout=15, max=500", "server": "Apache/2.2.15 (CentOS)", "connection": "Keep-Alive", "pragma": "no-cache", "cache-control": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0", "date": "Mon, 10 Nov 2014 01:39:03 GMT", "content-type": "text/html; charset=UTF-8", "x-pingback": "http://www.1world0x00.com/index.php/action/xmlrpc"}

>>> r.encoding
"UTF-8"

>>> r.status_code
200

下面这个比较长,是网页的内容,仅仅截取显示部分:

>>> print r.text

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1world0x00sec</title>
  <link rel="stylesheet" href="http://www.1world0x00.com/usr/themes/default/style.min.css">
  <link rel="canonical" href="http://www.1world0x00.com/" />
  <link rel="stylesheet" type="text/css" href="http://www.1world0x00.com/usr/plugins/CodeBox/css/codebox.css" />
  <meta name="description" content="爱生活,爱拉芳。不装逼还能做朋友。" />
  <meta name="keywords" content="php" />
  <link rel="pingback" href="http://www.1world0x00.com/index.php/action/xmlrpc" />

  ......

请求发出后,requests会基于http头部对相应的编码做出有根据的推测,当你访问r.text之时,requests会使用其推测的文本编码。你可以找出requests使用了什么编码,并且能够使用r.coding属性来改变它。

>>> r.content
"xefxbbxbfxefxbbxbf<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1world0x00sec</title>
    <link rel="stylesheet" href="http://www.1world0x00.com/usr/themes/default/style.min.css">
            <link ......

以二进制的方式打开服务器并返回数据。

post请求

requests发送post请求,通常你会想要发送一些编码为表单的数据——非常像一个html表单。要实现这个,只需要简单地传递一个字典给data参数。你的数据字典在发出请求时会自动编码为表单形式。

>>> import requests
>>> payload = {"key1":"value1","key2":"value2"}
>>> r = requests.post("http://httpbin.org/post")
>>> r1 = requests.post("http://httpbin.org/post", data=payload)

r没有加data的请求,看看效果:

r1是加了data的请求,看效果:

多了form项。喵。

http头部

>>> r.headers["content-type"]
"application/json"

注意,在引号里面的内容,不区分大小写"CONTENT-TYPE"也可以。

还能够自定义头部:

>>> r.headers["content-type"] = "adad"
>>> r.headers["content-type"]
"adad"

注意,当定制头部的时候,如果需要定制的项目有很多,需要用到数据类型为字典。


老齐备注

网上有一个更为详细叙述有关requests模块的网页,可以参考:http://requests-docs-cn.readthedocs.org/zh_CN/latest/index.html