渴望开始? 这个页面给了一个很好的建议如何使用Requests:
首先,确保:
1.Requests is installed
2.Requests is up-to-date
node2:/root#pip install Requests
Requirement already satisfied: Requests in /usr/local/python27/lib/python2.7/site-packages
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/python27/lib/python2.7/site-packages (from Requests)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/python27/lib/python2.7/site-packages (from Requests)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/python27/lib/python2.7/site-packages (from Requests)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/python27/lib/python2.7/site-packages/certifi-2018.4.16-py2.7.egg (from Requests)
You are using pip version 9.0.1, however version 10.0.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
You have mail in /var/spool/mail/root
让我们开始一些简单例子:
做一个请求:
现在,让我们尝试得到一个webpage,对于这个例子
做一个请求:
使用requests 是非常简答的
通过导入模式是非常简单的
import requests
现在, 让我们尝试去得到一个webpage,对于这个例子
import requests
import json
r = requests.get('https://api.github.com/events')
print type(r)
r= r.text
print type(r)
s=json.loads(r)
#print s
print type(s)
print s[0]
print '---------------------------------------'
print s[0]['payload']['head']
现在,我们有一个响应对象称为r 我们可以得到所有的信息从这个对象
import requests
import json
r = requests.get('https://api.github.com/events')
print type(r)
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a19.py
<class 'requests.models.Response'>
<Response [200]>
Requests的简单的API 意味着 所有的HTTP 请求的形式是显而易见的。
比如,如何进行一个HTTP POST请求
早URL 中传递参数
你通常希望发送某种类型的数据在URL的查询字符串里。
如果手动构建URL,这些值是以key/value 在一个问号后面 httpbin.org/get?key=val
齐全能够允许你提供那些参数作为字符串字典, 使用params关键
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get('http://httpbin.org/get', params=payload)
def ip_range(req):
print req.get_full_path()
start=req.GET['start']
stop=req.GET['stop']
startip=start.split('.')[-1];
stopip=stop.split('.')[-1];
f1=start.split('.')[0];
f2=start.split('.')[1];
f3=start.split('.')[2];
f4=f1+'.'+f2+'.'+f3
print f4
url(r'^api/ip_range/$', newview.ip_range),
http://192.168.137.3:9000/api/ip_range/?start=192.168.32.100&stop=192.168.32.110
import requests
import json
payload = {'start': '192.168.32.100', 'stop': '192.168.32.110'}
r = requests.get('http://192.168.137.3:9000/api/ip_range', params=payload)
print r.text
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a19.py
[["192.168.32.101", "dr-mysql", "env-backup"], ["192.168.32.102", "sftp", "env-test"], ["192.168.32.100", "unknown", "env-test"]]
Process finished with exit code 0
你可以看到URL 已经被正确的encoded 通过打印URL:
import requests
import json
payload = {'start': '192.168.32.100', 'stop': '192.168.32.110'}
r = requests.get('http://192.168.137.3:9000/api/ip_range', params=payload)
print r.text
print(r.url)
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a19.py
[["192.168.32.101", "dr-mysql", "env-backup"], ["192.168.32.102", "sftp", "env-test"], ["192.168.32.100", "unknown", "env-test"]]
http://192.168.137.3:9000/api/ip_range/?start=192.168.32.100&stop=192.168.32.110
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
payload = {'start': '测试', 'stop': '192.168.32.110'}
r = requests.get('http://192.168.137.3:9000/api/ip_range', params=payload)
print(r.url)
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a19.py
http://192.168.137.3:9000/api/ip_range/?start=%E6%B5%8B%E8%AF%95&stop=192.168.32.110
响应内容:
我们可以读取服务器响应的内容,再次考虑GitHub timeline again:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
r = requests.get('https://api.github.com/events')
print r.text
请求会自动的解码从服务器返回的内容,Unicode 字符集是无缝解码
请求发出后,Requests 会基于 HTTP 头部对响应的编码作出有根据的推测。当你访问 r.text 之时,Requests 会使用其推测的文本编码。你可以找出 Requests 使用了什么编码,并且能够使用 r.encoding 属性来改变它:
二进制响应内容
你也能以字节的方式请求响应提,对于非文本请求:
JSON 响应内容
Requests 中也有一个内置的JSON解码器,助你处理JSON数据:
import requests
import json
payload = {'start': '192.168.32.100', 'stop': '192.168.32.110'}
r = requests.get('http://192.168.137.3:9000/api/ip_range', params=payload)
print r.json()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a19.py
[[u'192.168.32.101', u'dr-mysql', u'env-backup'], [u'192.168.32.102', u'sftp', u'env-test'], [u'192.168.32.100', u'unknown', u'env-test']]
定制请求头:
如果你想为请求添加HTTP头部,只要简单地传递一个dict给headers参数就可以
例如,在前一个示例中我们没有指定content-type:
Accept
text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate, br
Accept-Language
zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Connection
keep-alive
Cookie
BAIDUID=0112786531976C6BEF22D3…_CK_SAM=1; PSINO=7; BD_HOME=0
Host
www.baidu.com
Upgrade-Insecure-Requests
1
User-Agent
Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/60.0
import requests
import json
url = 'https://baidu.com'
r=requests.get(url)
print r.headers
更加复杂的POST请求:
通常,你想要发送一些编码为表单形式的数据--非常像一个HTML表单,要实现这个,只需简单地产地一个字典给data参数。
你的数据字典 在发出请求时会自动编码为表单形式:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
s = requests.session()
print s.headers
url = "https://licai.yingyinglicai.com/user/login.htm"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Host': 'licai.yingyinglicai.com',
'Referer': 'https://licai.yingyinglicai.com/index.htm',
}
r = s.get(url, headers=headers)
r = r.text
# print s
print type(r)
r = r.encode('unicode-escape')
print type(r)
p = re.compile('.*token"s+value="(.*?)".*')
m = p.match(r)
token = m.group(1)
print token
login_url = 'https://licai.yingyinglicai.com/user/loginByPwd.do'
data = {'cell': '18072722237', 'password': 'xx', 'token': token}
# data = urllib.urlencode(data)
r = s.post(login_url, data=data, headers=headers)
print r.status_code
print r.headers['content-type']
print r.encoding
print r.text
POST一个多部分编码(Multipart-Encoded)的文件
Requests 使得上传多部分编码文件变得很简单:
Cookie:
如果某个响应中包含一些cookie,你可以快速访问它们:
JSESSIONID
httpOnly true
path /
value 2AA66E3B3802DAA7CA19C5A4EB336E8C
YYLCSJSESSIONID
domain yingyinglicai.com
expires 2018-05-30T08:41:16.000Z
path /
value 7fd936d6-a117-497b-a095-bc01f25d4c09