用python脚本遍历下载网站文件夹

因为需要,希望能下载http://www.xxx.us/上的所有xls/txt/pdf/rtf类型的资料,并按照相同路径保存文档。当然可以直接网上下载一个批量下载工具来搞定,但对于有DIY癖好的人来讲,当然自己写喽。





#coding=utf-8
from bs4 import BeautifulSoup
import os, sys, urllib2,time,random
 
path = os.getcwd()                     
root_path = os.path.join(path,u'TempDownload')
if not os.path.isdir(root_path):
    os.mkdir(root_path)
 
 
def download_loop(url, dl_path):
    print url
    content = urllib2.urlopen(url)
    soup = BeautifulSoup(content)
    link = soup.find_all('a')
    for download in link:
        flink = download.get('href')
        if flink.find(".pdf",len(flink)-4) <> -1 or flink.find(".xls",len(flink)-4) <> -1 or flink.find(".txt",len(flink)-4) <> -1 or flink.find(".xlsx",len(flink)-5) <> -1 or flink.find(".rtf",len(flink)-4) <> -1:
          print '+'+flink
          content2 = urllib2.urlopen(url+flink).read()
          with open(dl_path+'/'+flink,'wb') as code:
             code.write(content2)
        temp = download.get_text()
        if flink.find("/",len(flink)-1) <> -1 and temp <> u'Parent Directory':
          directory=str(flink[:-1])
          file_path= os.path.join(dl_path,directory.replace("%20"," "))
          if not os.path.isdir(file_path):
            os.mkdir(file_path)
          print '-'+flink + ' || path is:' + str(dl_path)
          new_url=url+flink
          download_loop(new_url, file_path)


##    print u'download completed'
     
download_loop('http://www.xxx.us/', root_path)
print "~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
raw_input("Press <Enter> To Quit!")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中实现遍历文件夹,可以使用两种方法。第一种方法是使用os.listdir()函数递归地遍历文件夹及其子文件夹中的所有文件。具体实现代码如下所示: ```python import os def get_filelist(dir): Filelist = [] for filename in os.listdir(dir): filepath = os.path.join(dir, filename) if os.path.isdir(filepath): Filelist.extend(get_filelist(filepath)) else: Filelist.append(filepath) return Filelist if __name__ == "__main__": dir = '具体路径' Filelist = get_filelist(dir) print(len(Filelist)) for file in Filelist: print(file) ``` 第二种方法是使用os.walk()函数,这种方法更简便。具体实现代码如下所示: ```python import os def get_filelist(dir): Filelist = [] for root, dirs, files in os.walk(dir): for file in files: filepath = os.path.join(root, file) Filelist.append(filepath) return Filelist if __name__ == "__main__": dir = '具体路径' Filelist = get_filelist(dir) print(len(Filelist)) for file in Filelist: print(file) ``` 以上代码均可以遍历指定文件夹及其子文件夹中的所有文件,并将文件名存储在一个列表中。你可以根据需要选择其中的一种方法进行实现。引用来源。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Ubuntun下,python脚本实现遍历文件夹,并获取特定文件](https://blog.csdn.net/qq_44085437/article/details/124421934)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python中如何遍历文件夹及其子文件夹中的所有文件(附实现代码)](https://blog.csdn.net/C_chuxin/article/details/83446602)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值