四:python接口之http請求html
python的強大之處在於提供了不少的標準庫以及第三庫,本文介紹urllib
和第三庫的requests。python
Urllib 定義了不少函數和類,這些函數和類可以幫助咱們在複雜的狀況下獲取url內容。複雜狀況— 基本的和深刻的驗證, 重定向, cookies 等等chrome
Urllib的GET請求代碼以下:json
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)
請求結果:api
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/playcrab/PycharmProjects/jiekouceshi/pachong.pycookie
200app
Date: Mon, 20 Feb 2017 08:08:36 GMTdom
Content-Type: text/html; charset=utf-8函數
Transfer-Encoding: chunkedpost
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請求的事例。