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 68 69 70
| """ Created on Wed Jul 18 13:06:07 2018
@author: zjk """
import re import requests import os
def dowmloadPic(html): url=r'http://textures.forrest.cz/' j=1 print('进程开始') dirpath=r'D:\zjk\2018暑假' kind=re.findall(' <a href="(\S+)" class="td-galitem-title">[\S ]+</a> ',html)
number=re.findall('([\d]+) pictures',html)
dirname='Texture' new_path=os.path.join(dirpath,dirname) if not os.path.isdir(new_path): os.makedirs(new_path) print("创建") sum=len(kind) pic_num=1 for i in range(sum): for j in range(int(number[i])):
dirtype= re.findall("spgmGal=([\S]+)",kind[i])[0] branch_path=os.path.join(new_path,dirtype) if not os.path.isdir(branch_path): os.makedirs(branch_path) print("创建"+branch_path+"文件夹") find_img=r'http://textures.forrest.cz/index.php?spgmGal=%s&spgmPic=%d#spgmPicture'%(dirtype,j) html=requests.get(find_img).text img_url=re.findall('<img id="picture" src="([\S]+)" ',html)[0]
pic_name=img_url.split('/')[-1]
pic_url=url+img_url print('正在下载第' + str(pic_num) + '张图片') try: pic = requests.get(pic_url, timeout=10) except requests.exceptions.ConnectionError: print('【错误】当前图片无法下载') continue dir = branch_path + '\\' + pic_name fp = open(dir, 'wb') fp.write(pic.content) fp.close() pic_num += 1 if __name__ == '__main__': url =r'http://textures.forrest.cz/index.php?' result=requests.get(url) dowmloadPic(result.text)
|