Python3批量下载.dat和.hea文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weifuliu/article/details/82908551

在杭州电子科技大学的读研的哥哥研究项目需要在一个网站上下载数据进行数据分析,总共4000多份文档数据,若是手工点击链接下载的话,不知道要下载到猴年马月了,还好我哥知道我会爬虫,嘿嘿,这时候就该展现我Python爬虫威力了。

数据存放的地址

代码

#https://physionet.org/physiobank/database/hbedb/BDS00001.dat

import requests
from bs4 import BeautifulSoup
import re
import os
import urllib


def getHTMLText(url):
    try:
        r=requests.get(url)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "产生异常"

def getBat(html):
    reg = r'href=".*.bat"'
    imgre = re.compile(reg) 
    imglist = re.findall(imgre, html)
    return imglist


if __name__=="__main__":
    lst=[]
    url="https://physionet.org/physiobank/database/hbedb/"
    text=getHTMLText(url)
    soup=BeautifulSoup(text,"html.parser")
    files=soup.find_all('a')
    for i in files:
        try:
            href=i.attrs['href']
            lst.append(re.findall(r'BDS.*.hea',href)[0])
        except:
            continue
    for l in lst:
        link="https://physionet.org/physiobank/database/hbedb/"+l
        ff=requests.get(link)
        print(link+"正在下载")
        try:
            f = open('C:\\Users\\Administrator\\Desktop\\lwf\\Spider\\csdn\\hea\\'+ str(l), 'wb')
            f.write(ff.content)
            f.close()
        except Exception as e:
            print("失败")
           

效果图
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/82908551