四:python接口之http请求
python的强大之处在于提供了很多的标准库以及第三库,本文介绍urllib
和第三库的requests。
Urllib 定义了很多函数和类,这些函数和类能够帮助我们在复杂的情况下获取url内容。复杂情况— 基本的和深入的验证, 重定向, cookies 等等
Urllib的GET请求代码如下:
import urllib.request url=‘http://www.baidu.com‘ response=urllib.request.Request(url=url) html=urllib.request.urlopen(response) print(html.getcode()) print(html.headers)
请求结果:
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/playcrab/PycharmProjects/jiekouceshi/pachong.py
200
Date: Mon, 20 Feb 2017 08:08:36 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: Close
Set-Cookie: BAIDUID=D3E5547ACC26D3908EBB29522BABCCD4:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=D3E5547ACC26D3908EBB29522BABCCD4; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1487578116; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=21935_1455_21090_17001_22036; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Cxy_all: baidu+547441cee80f5b2514d2439b86d8b151
Expires: Mon, 20 Feb 2017 08:08:33 GMT
X-Powered-By: HPHP
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
BDPAGETYPE: 1
BDQID: 0xc3d33f280001f43e
BDUSERID: 0
Urllib的Post请求,代码:
import urllib.request import urllib.parse url=‘http://www.tuling123.com/openapi/api‘ data={"key": "your", "info": ‘你好‘} data=urllib.parse.urlencode(data).encode(‘utf-8‘) re=urllib.request.Request(url,data) html=urllib.request.urlopen(re) print(html.getcode(),html.msg) print(html.read())
结果:
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/playcrab/PycharmProjects/jiekouceshi/pachong.py
200 OK
b‘{"code":40001,"text":"\xe4\xba\xb2\xe7\x88\xb1\xe7\x9a\x84\xef\xbc\x8ckey\xe4\xb8\x8d\xe5\xaf\xb9\xe5\x93\xa6\xe3\x80\x82"}‘
(注:这里不是乱码是输出格式的问题。)
下面介绍下requests库的http请求、
GET请求:
import requests r = requests.get(‘https://www.baidu.com‘) print(r.headers)
结果:
结果:
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/playcrab/PycharmProjects/jiekouceshi/pachong.py
200 OK
b‘{"code":40001,"text":"\xe4\xba\xb2\xe7\x88\xb1\xe7\x9a\x84\xef\xbc\x8ckey\xe4\xb8\x8d\xe5\xaf\xb9\xe5\x93\xa6\xe3\x80\x82"}‘
(注:这里不是乱码是输出格式的问题。)
下面介绍下requests库的http请求、
GET请求:
import requests r = requests.get(‘https://www.baidu.com‘) print(r.headers)
结果:
{‘Last-Modified‘: ‘Mon, 23 Jan 2017 13:23:55 GMT‘, ‘Transfer-Encoding‘: ‘chunked‘, ‘Pragma‘: ‘no-cache‘, ‘Cache-Control‘: ‘private, no-cache, no-store, proxy-revalidate, no-transform‘, ‘Content-Encoding‘: ‘gzip‘, ‘Date‘: ‘Mon, 20 Feb 2017 08:30:29 GMT‘, ‘Server‘: ‘bfe/1.0.8.18‘, ‘Connection‘: ‘keep-alive‘, ‘Content-Type‘: ‘text/html‘, ‘Set-Cookie‘: ‘BDORZ=27315; max-age=86400; domain=.baidu.com; path=/, __bsi=12827760870170119103_00_7_N_N_2_0301_002F_N_N_N_0; expires=Mon, 20-Feb-17 08:30:34 GMT; domain=www.baidu.com; path=/‘}
POST请求:
import requests payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘} r = requests.post("http://httpbin.org/post", data=payload) print(r.text)
@font-face { font-family: "Times"; }@font-face { font-family: "宋体"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "@宋体"; }@font-face { font-family: "Cambria"; }@font-face { font-family: "Menlo"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Cambria; }p { margin-right: 0cm; margin-left: 0cm; font-size: 10pt; font-family: Times; }pre { margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: Courier; }span.HTML { font-family: Courier; }.MsoChpDefault { font-size: 10pt; font-family: Cambria; }div.WordSection1 { }
结果:
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.10.0"
},
"json": null,
"origin": "180.87.10.156",
"url": "http://httpbin.org/post"
以上是利用urllib和requests发送GET和POST请求的事例。