Python # 一个api接口调用POST请求

###

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

###

posted @ 2017-09-18 22:52  陈奕迅-可以了  阅读(10479)  评论(0编辑  收藏  举报