python Requsets下載開(kāi)源網(wǎng)站的代碼(帶索引 數(shù)據(jù))
環(huán)境搭建
python 3.x
requests 包
re 包
gooey包 (用于可視化)
代碼
import requests import re import os from gooey import Gooey, GooeyParser import time s = requests.Session() def judgeTypeOfPath(name): ''' 判斷該路徑是文件還是文件夾 :param name: 路徑名稱 :return:True->文件;False->文件夾 ''' if name[-1] == '/': return False else: return True def makeDirOfPath(path): ''' 創(chuàng)建文件夾 :param path: 文件夾名稱以及路徑 :return: True->創(chuàng)建成功;False->創(chuàng)建失敗 ''' if not os.path.isdir(path): os.mkdir(path) if not os.path.isdir(path): return False return True def getPath(url): ''' 獲取網(wǎng)頁(yè)路徑列表 :param url: 當(dāng)前網(wǎng)頁(yè)路徑 :return: 路徑列表 ''' baseResponse = s.get(url=url, stream=True,verify=False).text listOfDirOrFilesTemp = re.findall(r'<li><a href=".*?" rel="external nofollow" >', baseResponse) listOfDirOrFiles = [] for i in range(len(listOfDirOrFilesTemp)): listOfDirOrFiles.append(listOfDirOrFilesTemp[i].split("\"")[1]) return listOfDirOrFiles[1:len(listOfDirOrFiles) + 1] def rfSearch(listOfPath,url, nowPath): ''' 遞歸尋找目錄、路徑,并下載文件 :param listOfPath: 當(dāng)前目錄下文件以及文件夾目錄列表 :param nowPath: 現(xiàn)在所在路徑 :return: ''' newList = listOfPath[:] if not newList: return for i in range(len(newList)): if not judgeTypeOfPath(newList[i]): u = nowPath + newList[i][0:len(newList[i])] makeDirOfPath(u) tempPath=nowPath + newList[i][0:len(newList[i])+1] tempUrl=url+newList[i][0:len(newList[i])+1] u=getPath(tempUrl) rfSearch(u,tempUrl,tempPath) else: print(f'開(kāi)始下載{newList[i]}...') t1=time.time() u = nowPath + newList[i] m=url+newList[i] if not os.path.exists(u): r = s.get(m, stream=True,verify=False) f = open(u, "wb") for chunk in r.iter_content(chunk_size=10240): if chunk: f.write(chunk) f.close() t2=time.time() print(f'{newList[i]}下載完成\t\t用時(shí) {t2-t1}') @Gooey( program_name='isric數(shù)據(jù)下載器', encoding="utf-8", ) def main(): parser = GooeyParser(description="isric數(shù)據(jù)下載器") parser.add_argument('--url',default=r'https://files.isric.org/soilgrids/latest/data/') parser.add_argument('--path', widget="DirChooser", default=r'F:/isricData/') args = parser.parse_args() url=args.url nowPath = args.path u = getPath(url) rfSearch(u, url,nowPath) ###如果不需要可視化,則不用gooey,可以將上面部分替換如下 #@Gooey( # program_name='isric數(shù)據(jù)下載器', # encoding="utf-8", ) #上面三行刪除即可 ###main函數(shù)替換成下面部分: # def main(): # url=r'https://files.isric.org/soilgrids/latest/data/'#在此處修改地址鏈接 # nowPath = r'F:/isricData/'#在此處修改文件保存地址 # u = getPath(url) # rfSearch(u, url,nowPath) if __name__ == "__main__": main()
到此這篇關(guān)于python Requsets下載開(kāi)源網(wǎng)站的代碼(帶索引 數(shù)據(jù))的文章就介紹到這了,更多相關(guān)python Requsets下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用python實(shí)現(xiàn)批量重命名文件的代碼
任務(wù)很簡(jiǎn)單,某個(gè)目錄下面有幾千個(gè)文件,某些文件沒(méi)有后綴名,現(xiàn)在的任務(wù)就是將所有的沒(méi)有后綴名的文件加上后綴名,python有現(xiàn)成的函數(shù)可以實(shí)現(xiàn),但是在實(shí)現(xiàn)過(guò)程中遇到幾個(gè)問(wèn)題,分享一下解決方法2012-05-05python樹(shù)的同構(gòu)學(xué)習(xí)筆記
在本篇文章里小編給大家整理的是一篇關(guān)于python樹(shù)的同構(gòu)學(xué)習(xí)筆記以及相關(guān)實(shí)例代碼內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-09-09python中字典dict排序sorted的實(shí)現(xiàn)
本文主要介紹了python中字典dict排序sorted的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Webots下載安裝?+?Pycharm聯(lián)調(diào)使用教程
Webots是一個(gè)開(kāi)源的三維移動(dòng)機(jī)器人模擬器,它最初是作為研究移動(dòng)機(jī)器人中各種控制算法的研究工具開(kāi)發(fā)的,自2018年12月起,Webots作為開(kāi)源軟件發(fā)布,并獲得Apache 2.0許可證,這篇文章主要介紹了Webots下載安裝?+?Pycharm聯(lián)調(diào)?,需要的朋友可以參考下2023-02-02python sys.stdin和sys.stdout的用法說(shuō)明
這篇文章主要介紹了python sys.stdin和sys.stdout的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Python八大常見(jiàn)排序算法定義、實(shí)現(xiàn)及時(shí)間消耗效率分析
這篇文章主要介紹了Python八大常見(jiàn)排序算法定義、實(shí)現(xiàn)及時(shí)間消耗效率分析,結(jié)合具體實(shí)例形式對(duì)比分析了冒泡排序、直接插入排序、選擇排序、歸并排序、希爾排序、桶排序、堆排序等排序算法的使用與執(zhí)行效率,需要的朋友可以參考下2018-04-04