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
| import requests import re import os
class GetImage(object): def __init__(self, url): self.url = url self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' } self.dir_path = os.path.dirname(os.path.abspath(__file__)) self.path = self.dir_path+'/imgs' isExists = os.path.exists(self.dir_path+'/imgs') if not isExists: os.makedirs(self.path)
def download(self, url): try: res = requests.get(url, headers=self.headers) return res except Exception as e: print(url+'下载失败,原因:'+e.args)
def save(self, res_img, file_name): if res_img: with open(file_name, 'wb') as f: f.write(res_img.content) print(url+'下载成功')
def run(self): res_img = self.download(self.url) name = self.url.strip().split('/').pop() file_name = self.path+'/'+name self.save(res_img, file_name)
if __name__ == '__main__': url_list = [] Letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] for i in Letter: for j in Letter: url_list.append( "http://pic.c-ctrip.com/AssetCatalog/airline/32/" + i + j + ".png")
for url in url_list: print(url) text = GetImage(url) text.run()
|