调用api,下载图片

import requests,json
import urllib

url='http://apis.baidu.com/txapi/mvtp/meinv'
path='F:\\screen\\baidu\\'
headers={'apikey':XXXX}
page_links=[]
names=[]
def get_url(num):
    payload={'num':num}

    page=requests.get(url,params=payload,headers=headers)

    data=page.json()
    for i in range(1,num):
         page_links.append(data['newslist'][i]['picUrl'])
         names.append(data['newslist'][i]['title'])

def get_file(links,names):
    for link,name in zip(links,names):
        save_img(link,name)


def save_img(url,name):
    urllib.request.urlretrieve(url,path + name+'.'+url.split('.')[-1])
    print ('Done')
get_url(10)

get_file(page_links,names)

 

posted @ 2016-11-10 16:23  zhgmyron  阅读(793)  评论(0编辑  收藏  举报