[Python]_[批量下载网站文件]

本文介绍了一个简单的Python脚本,该脚本可以用来批量下载指定网站上的特定类型文件(如PDF)。通过解析网页内容并提取所需的文件链接,脚本能够自动创建必要的目录结构并将文件保存到本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


场景:

1.有时候需要下载某个网站上提供的所有pdf文件,貌似没发现哟下载工具提供。


#! encoding=utf-8

import urllib2
import re
import os

def Download(url,output):
    print "downloading..."+url
    response = urllib2.urlopen(url)
    resourceFile = open(output,"wb")
    resourceFile.write(response.read())
    resourceFile.close()
    print "downloaded"

def Action(url,ext = "pdf",output = "."):
    
    #1.domain
    index = url.rfind("/");
    domain = url[0:index+1];
    print domain
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    
    #2.content
    content = response.read()
#    print content
    
    #3.resource
    mode = '\"([^\"]+'+ext+')\"'
    pattern = re.compile(mode)
    strMatch = pattern.findall(content)
    size = len(strMatch)
    print "file num: "+str(size)
    for i in range(0,size,1):
#        print strMatch[i]
        one = strMatch[i]
        partIndex = one.rfind('/')
        if not one.startswith('http://'):
            if -1!=partIndex:
                directDir = one[0:partIndex+1]
            else:
                directDir = ""
#            print directDir
            try:
                os.makedirs(output+"/"+directDir)
            except Exception,e:
                pass
            fileUrl = domain+one
            fileOutput = output+"/"+one
            print fileUrl
            print fileOutput
            Download(fileUrl,fileOutput)
        else:
            print one
            print "........."
            print one[partIndex:]
            fileOutput = output+"/"+one[partIndex:]
            print fileOutput
            Download(one,fileOutput)
    #5.download

if __name__=='__main__':
    print "download"
    url = "http://compgeom.cs.uiuc.edu/~jeffe/teaching/algorithms/";
    Action("http://tech.qq.com/","jpg");
    
    
    

    
    
    



使用matlab批量下载网站上的文件-MatlabUrlDownloadToFile.m 前几天美国数学建模成绩出了,我们按照网址下载了自己的证书,另外想看看学校今年的获奖情况,由于学校的参赛队伍数众多,一个一个下载证书再统计显得麻烦,于是想自己用matlab开发一个小程序以实现同类型文件批量下载。很多网站上有介绍使用Windows API 函数URLDownloadToFile,但是这个在VC上实现容易,要把它移植到matlab上还得使用MEX,有点麻烦了。这几天一直徘徊个大小网站,没有找到相关信息。最后还是自己使用lookfor找到需要的函UrlWrite,调用这个函数轻松搞定批量下载! 以下是代码: %使用matlab下载网站上的文件 %使用urlwrite函数下载网站上的文件 %特别适用于批量下载同类型的文件 % Created by longwen36 2011-4-16 %使用urlwrite实现批量下载 %以下载2011美赛数模证书为例 clc,clear; num = 11701:11710; URLs = cell); folder_filenames = cell); filenames = cell); for idx  = 1:length;     URLs{idx} = sprintf);     filenames{idx} = sprintf); end tic; for idx = 1:length;     fprintf;     [f, status] = urlwrite;     if status == 1;         fprintf;     else         fprintf;     end     end etime = toc; fprintf,etime); 复制代码 下载速度还挺快的,才发现,matlab功能确实很强大,函数很齐全。利用这个函数,加以改进,编写GUI,就可以做个简易的批量下载器了! 感兴趣的同仁可以试试!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peter(阿斯拉达)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值