這是事情,我正在做一個腳本,從不同的站點下載文件。事情是,我無法弄清楚爲什麼它拋出我這個錯誤,而如果我把我的瀏覽器相同的網址,它讓我下載文件。還有其他的網站工作正常。所以......這裏是代碼:嘗試下載文件時出現http錯誤400 urllib2
import os
from bs4 import BeautifulSoup
import time
import urllib2
f = urllib2.Request(url)
f.add_header('User-Agent', 'Mozilla/5.0 Windows NT 6.3; WOW64; rv:34.0')
request = urllib2.urlopen(f)
data = request.read()
soup = BeautifulSoup(data, 'html.parser')
p_name = soup.find('h2', id="searchResults").contents[0]
if not os.path.exists(p_name):
os.makedirs(p_name)
for a in soup.find_all('a', href="#register"):
f = a["data-durl"]
#Following two lines just prepares file name
n = len(f.split("/"))
n_file = f.split("/")[n-1]
path_file = p_name+"\\"+n_file
if os.path.isfile(path_file):
print "Firmware already downloaded. skipping it"
else:
print "Downloading "+ path_file
link = urllib2.urlopen(f)
datos = link.read()
#print "[+] Downloading firmware %s" % n_file
#n_archivo = "Archivo"+str(b)+".zip"
with open(path_file, "wb") as code:
code.write(datos)
time.sleep(2)
這個網址就是不會用這個腳本工作:Non working url 但是這一個正常工作working url
希望你能幫助我。
編輯:我添加了我用於此的庫。 和堆棧跟蹤 我發現錯誤!問題是它試圖下載的文件名稱上的空格。使用f.replace(「」,「%20」)應該可以正常工作:)
這兩個網址都適合我。你從哪裏得到錯誤?請發佈完整的堆棧跟蹤。 – Selcuk