Python 递归下载目录中的特定文件
Python 递归下载目录中的特定文件,python,urllib2,Python,Urllib2,此文件夹包含许多子目录和文件。我只想使用Python下载maven metadata.xml文件。我试过了,但它不会递归遍历子目录。我建议也使用beautiful soup。。你可以这样做,而我的测试,如果它是一个目录,是非常非常简单的(只是,如果链接带有“/”): 你试过使用吗? from urllib.request import urlopen import re from bs4 import BeautifulSoup import requests def isDirectory
此文件夹包含许多子目录和文件。我只想使用Python下载
maven metadata.xml
文件。我试过了,但它不会递归遍历子目录。我建议也使用beautiful soup。。你可以这样做,而我的测试,如果它是一个目录,是非常非常简单的(只是,如果链接带有“/”):
你试过使用吗?
from urllib.request import urlopen
import re
from bs4 import BeautifulSoup
import requests
def isDirectory(url):
if(url.endswith('/')):
return True
else:
return False
def findLinks(url):
page = requests.get(url).content
bsObj = BeautifulSoup(page, 'html.parser')
maybe_directories = bsObj.findAll('a', href=True)
for link in maybe_directories:
print(link['href'])
print(isDirectory(link['href']))
if(isDirectory(link['href'])):
newUrl = url + link['href']
findLinks(newUrl) #recursion happening here
else:
if(link['href'].endswith('maven-metadata.xml')):
print("GOTCHA!") #now safe and download
startUrl = "https://repo1.maven.org/maven2/"
findLinks(startUrl)