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

###

转载于:https://www.cnblogs.com/lwsup/p/7545802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值