tfp = open(filename, 'wb')OSError:[Errno 22}無效參數:'downloaded/misc/jquery.js?v = 1.4.4'
OSError: [Errno 22} Invalid argument: 'downloaded/misc/jquery.js?v=1.4.4'
任何人都可以幫我解決這個錯誤嗎?我認爲它與jquery.js?v=1.4.4
無效。我是python的新手;我很抱歉,如果我失去了一些明顯的東西。
下面是代碼:
import os
from urllib.request import urlretrieve
from urllib.request import urlopen
from bs4 import BeautifulSoup
downloadDirectory = "downloaded"
baseUrl = "http://pythonscraping.com"
def getAbsoluteURL(baseUrl, source):
if source.startswith("http://www."):
url = "http://"+source[11:]
elif source.startswith("http://"):
url = source
elif source.startswith("www."):
url = source[4:]
url = "http://"+source
else:
url = baseUrl+"/"+source
if baseUrl not in url:
return None
return url
def getDownloadPath(baseUrl, absoluteUrl, downloadDirectory):
path = absoluteUrl.replace("www.", "")
path = path.replace(baseUrl, "")
path = downloadDirectory+path
directory = os.path.dirname(path)
if not os.path.exists(directory):
os.makedirs(directory)
return path
html = urlopen("http://www.pythonscraping.com")
bsObj = BeautifulSoup(html, "html.parser")
downloadList = bsObj.findAll(src=True)
for download in downloadList:
fileUrl = getAbsoluteURL(baseUrl, download["src"])
if fileUrl is not None:
print(fileUrl)
urlretrieve(fileUrl, getDownloadPath(baseUrl, fileUrl, downloadDirectory))
它不是下載一個有效的文件,也許這是不正確的鏈接,下載文件。 – Arman
是的,這是有道理的。謝謝。 –