码迷,mamicode.com
首页 > 编程语言 > 详细

python调用百度API

时间:2016-04-28 00:19:17      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

from urllib.request import urlopen

import requests
import json


url = "http://apis.baidu.com/txapi/mvtp/meinv"         #API
req = requests.get(url)
headers= {‘apikey‘:‘自己的apikey‘}  #自己的apikey
params = {‘num‘:‘5‘}                                    #请求参数(urlParam) :
r = requests.get(url,params=params,headers=headers)
print(r)

print(r)
def SaveImage(ImageUrl,ImgName= ‘default.jpg‘):
    response = requests.get(ImageUrl,stream = True)
    image = response.content

    dst = ‘/Users/Alan/desktop/BaiDUImages/‘
    path  = dst+ImgName
    print(‘Save the file:‘,path)
    with open(path,‘wb‘) as img:
        img.write(image)
def run():
    for line in r[‘newslist‘]:
        title = line[‘title‘]
        picUrl = line[‘picUrl‘]
        SaveImage(picUrl,ImgName=title+‘.jpg‘)
run()

  

python调用百度API

标签:

原文地址:http://www.cnblogs.com/alan-babyblog/p/5440974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!