下载壁纸
发布时间:2020-12-17 17:17:28 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ?import reimport osimport urllib.requesthome = "http://www.gamersky.com/ent/wp/"i_count = 5 # 爬取的列表数量def downImg(url,title): if not o
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 ?import re import os import urllib.request home = "http://www.gamersky.com/ent/wp/" i_count = 5 # 爬取的列表数量 def downImg(url,title): if not os.path.exists(title): os.makedirs(title) page = urllib.request.urlopen(url).read() try: page = page.decode('utf-8') except: print("try gbk code") page = page.decode('gbk') #s_key = 'src="(http://img1.gamersky.com/.+?.jpg)"' s_key = 'href="http://www.gamersky.com/showimage/id_gamersky.shtml?(http://img1.gamersky.com/.+?.jpg)"' re_c = re.compile(s_key) ls2 = re.findall(re_c,page) for l2 in ls2: try: (p2,f2) = os.path.split(l2) if os.path.exists(title + "/" + f2): continue print(l2) urllib.request.urlretrieve(l2,title + "/" + f2) except: print('down image error!') if __name__ == '__main__': try: url = home page = urllib.request.urlopen(url).read() try: page = page.decode('utf-8') except: print("try gbk code") page = page.decode('gbk') print(len(page)) s_key = 'href="(http://www.gamersky.com/ent.+?.shtml)"' re_c = re.compile(s_key) ls = re.findall(re_c,page) i = 0 for l in ls: if i >= i_count: break i += 1 print("(" + str(i) + "/" + str(i_count) + ") " + l) try: (path,file) = os.path.split(l) title = file.replace('.shtml','') page = urllib.request.urlopen(l).read() page = page.decode('utf-8') url2 = l.replace('.shtml','') s_key = 'href="(' + url2 + '.+?)"' re_c = re.compile(s_key) ls2 = re.findall(re_c,page) j = 0 for l2 in ls2: j += 1 print("(" + str(j) + "/" + str(len(ls2)) + ") " + l2) try: downImg(l2,title) except: print('error II !') except: print('error!') except: print("read index error!") print('finish!') 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |