Python下载文件到指定文件夹
import os
import urllib.request
import requests as rb
from bs4 import BeautifulSoup
def file_downloand(fileUrl):
basedir = "C:/Users/Administrator/Downloads/fileDownload"
if not os.path.exists(basedir):
os.makedirs(basedir)
file_path = basedir + '/' + fileUrl[fileUrl.rfind("/") + 1:len(fileUrl)]
if os.path.exists(file_path) == False:
try:
print("下载链接:" + fileUrl)
urllib.request.urlretrieve(fileUrl, filename=file_path)
print("成功下载文件")
except IOError as exception_first:
print(1, exception_first)
except Exception as exception_second:
print(2, exception_second)
else:
print("文件已经存在!")
def get_download_url_from_page(base_url):
fileList = []
data = rb.get(base_url)
soup = BeautifulSoup(data.text, "html.parser")
links = []
for link in soup.find_all("a"):
print(link.get("href"))
if link.get("href").count('java') == 1:
links.append(link.get("href"))
for link_url in links:
data_file = rb.get(base_url + link_url)
soup_file = BeautifulSoup(data_file.text, "html.parser")
for link_file in soup_file.find_all("a"):
if str(link_file.get("href")).count('jar') == 1 and str(link_file.get("href")).count('jar.asc') != 1:
fileList.append(base_url + link_url + link_file.get("href"))
return fileList
fileList = get_download_url_from_page("https://mirrors.tuna.tsinghua.edu.cn/mariadb/")
for fileUrl in fileList:
file_downloand(fileUrl)