1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| """根据搜索词下载百度图片""" import re import sys import urllib import os import requests def getPage(keyword,page,n): page=page*n keyword=urllib.parse.quote(keyword, safe='/') url_begin= "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=" url = url_begin+ keyword + "&pn=" +str(page) + "&gsm="+str(hex(page))+"&ct=&ic=0&lm=-1&width=0&height=0" return url def get_onepage_urls(onepageurl): try: html = requests.get(onepageurl).text except Exception as e: print(e) pic_urls = [] return pic_urls pic_urls = re.findall('"objURL":"(.*?)",', html, re.S) return pic_urls def down_pic(pic_urls, keyword): """给出图片链接列表, 下载所有图片""" for i, pic_url in enumerate(pic_urls): try: pic = requests.get(pic_url, timeout=15) string =str(i + 1) + '.jpg' if os.path.exists('./' + keyword): with open('./' + keyword + '/' + string, 'wb') as f: f.write(pic.content) print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url))) else: os.mkdir('./' + keyword) with open('./' + keyword + '/' + string, 'wb') as f: f.write(pic.content) print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url))) except Exception as e: print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url))) print(e) continue if __name__ == '__main__': keyword = '天海翼' page_begin=0 page_number=30 image_number=3 all_pic_urls = [] while 1: if page_begin>image_number: break print("第%d次请求数据",[page_begin]) url=getPage(keyword,page_begin,page_number) onepage_urls= get_onepage_urls(url) page_begin += 1 all_pic_urls.extend(onepage_urls) down_pic(list(set(all_pic_urls)), keyword)
|