###
import urllib2,json url='http://xxx.xxx.com.cn/api/v1' data=json.dumps({'organId':'1'}) #數據 headers={'Content-Type':'application/json'} #頭部 request = urllib2.Request(url,data,headers) #urllib2用一個Request對象來映射你提出的HTTP請求 print request response = urllib2.urlopen(request) #通過調用urlopen並傳入Request對象,將返回一個相關請求response對象 print response message = response.read() #這個應答對象如同一個文件對象,所以你可以在Response中調用.read() print message
curl命令
curl -X POST -H "Content-Type:application/json" -d {"organId":"1"} http://xxx.xxx.com.cn/api/
###
urllib2另外一種用法:
import urllib2,json,urllib response= urllib2.urlopen('http://www.ttlsa.com/') html = response.read() print html req = urllib2.Request('http://www.ttlsa.com/') response = urllib2.urlopen(req) the_page = response.read() print the_page print urllib2.Request.get_method(req)
###
import urllib2 import urllib values = {'query' : '捷豹'} url_values = urllib.urlencode(values) url = 'https://www.sogou.com/web' full_url = url + '?' + url_values print full_url req = urllib2.Request(full_url) res = urllib2.urlopen(req) page = res.read() print page
###