欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python Requsets下载开源网站的代码(带索引 数据)

时间:2021-12-07 12:17:33|栏目:Python代码|点击:

环境搭建

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):
    '''
    创建文件夹
    :param path: 文件夹名称以及路径
    :return: True->创建成功;False->创建失败
    '''
    if not os.path.isdir(path):
        os.mkdir(path)
    if not os.path.isdir(path):
        return False
    return True

def getPath(url):
    '''
    获取网页路径列表
    :param url: 当前网页路径
    :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: 当前目录下文件以及文件夹目录列表
    :param nowPath: 现在所在路径
    :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'开始下载{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用时  {t2-t1}')

@Gooey(
    program_name='isric数据下载器',
    encoding="utf-8", )
def main():
    parser = GooeyParser(description="isric数据下载器")
    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数据下载器',
#   encoding="utf-8", )
#上面三行删除即可
###main函数替换成下面部分:
# 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()

上一篇:Python 做曲线拟合和求积分的方法

栏    目:Python代码

下一篇:Ubuntu16.04 安装多个python版本的问题及解决方法

本文标题:python Requsets下载开源网站的代码(带索引 数据)

本文地址:http://www.codeinn.net/misctech/186058.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有