参考:1.http://www.myexception.cn/javascript/1467350.html
百度图片api:
http://image.baidu.com/i?tn=baiduimagejson&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1349413075627_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&word=girl&rn=2&pn=1
这个地址比较长,删除一下参数:
http://image.baidu.com/i?tn=baiduimagejson&width=&height=&word=girl&rn=10&pn=2几个关键参数:
width&height:长宽
word:查询关键词
rn:每页显示图片数量
pn:图片显示的页码
<pre name="code" class="javascript">参考返回结果如下 { "queryEnc": "girl", "queryExt": "girl", "listNum": 1999, "displayNum": 21929725, "bdFmtDispNum": "约21,900,000", "bdSearchTime": "6.000", "bdIsClustered": "1", "data": [ { "thumbURL": "http://t10.baidu.com/it/u=1025587248,1458534674&fm=59", "middleURL": "", "largeTnImageUrl": "http://t12.baidu.com/it/u=924334832,1400490131&fm=59", "hasLarge": 0, "hoverURL": "http://t11.baidu.com/it/u=1001163760,1466976252&fm=59", "pageNum": 1, "objURL": "http://www.widewallpapers.ru/mod/girls/3/1920x1200/hot-girl-wallpaper-1920x1200-008.jpg", "fromURL": "http://hdw.eweb4.com/out/687817.html", "fromURLHost": "http://hdw.eweb4.com", "currentIndex": "0", "width": 1920, "height": 1200, "type": "jpg", "filesize": "715", "bdSrcType": "5", "di": "0", "is": "0", "bdSetImgNum": 0, "bdImgnewsDate": "2013-02-01 13:22:13", "fromPageTitle": "<strong>girl</strong>", "fromPageTitleEnc": "<strong>girl</strong>", "bdSourceName": "", "bdFromPageTitlePrefix": "", "token": "0" }, { "thumbURL": "http://t10.baidu.com/it/u=642396156,1965090283&fm=59", "middleURL": "", "largeTnImageUrl": "http://t10.baidu.com/it/u=636229272,1901199173&fm=59", "hasLarge": 0, "hoverURL": "http://t10.baidu.com/it/u=657566964,1952941079&fm=59", "pageNum": 2, "objURL": "http://www.mrwallpaper.com/wallpapers/blonde-girl-green-eyes-1920x1200.jpg", "fromURL": "http://hdw.eweb4.com/out/650129.html", "fromURLHost": "http://hdw.eweb4.com", "currentIndex": "0", "width": 1920, "height": 1200, "type": "jpg", "filesize": "436", "bdSrcType": "5", "di": "0", "is": "0", "bdSetImgNum": 0, "bdImgnewsDate": "2013-02-01 13:22:13", "fromPageTitle": "<strong>girl</strong>", "fromPageTitleEnc": "<strong>girl</strong>", "bdSourceName": "", "bdFromPageTitlePrefix": "", "token": "0" }, {} ] }
下面是Pyhton代码:
#get_girls_image from urllib2 import urlopen from json import load,dumps import urllib,os def main(): url = 'http://image.baidu.com/i?' word = raw_input('please input a key word about image u want to search:') params = 'tn=baiduimagejson&width=&height=&ie=utf8&oe=utf-8&ic=0&rn=20&pn=0'+'&'+ 'word='+ word #得到完整的地址 url += params #以json方式打开图片api地址 page = urlopen(url) info = load(page) writeinfo(info) if 'tag1' in info: if 'tag2' in info: print info['tag1']+info['tag2']+' totalNum:' + str(info['totalNum']) dirname = os.getcwd()+'\\'+info['tag1']+info['tag2']+'\\' elif 'queryEnc' in info: dirname = os.getcwd()+'\\'+info['queryEnc']+'\\' else: print 'wrong input,no image' dirname =os.getcwd()+'\\'+'girl\\' imgs = getImgUrl(info) if not os.path.exists(dirname): os.mkdir(dirname) for k,v in imgs.iteritems(): print k,v downloadImg(v,dirname+k) #把地址信息写入文件 def writeinfo(dat): ff = open('image_info_temp.json','w') ff.write(dumps(dat,indent=4)) ff.close() #获取图片地址 def getImgUrl(dat): kandv = [] idnum,imgurl = [],[] #dat is json format if 'data' in dat: temp =dat['data'] elif 'imgs' in dat: temp =dat['imgs'] for dd in temp: if 'image_url' in dd: #print 'image_url: ' + dd['image_url'] + '\n' imgurl.append(dd['image_url']) idnum.append(os.path.split(dd['image_url'])[1]) elif 'objURL' in dd: imgurl.append(dd['objURL']) idnum.append(os.path.split(dd['objURL'])[1]) kandv = zip(idnum,imgurl) return dict(kandv) #下载图片 def downloadImg(url0,name0): urllib.urlretrieve(url0,'%s.jpg'%name0) if __name__ == '__main__': main()