2017-04-03 259 views
0

我是python的新手,試圖編寫腳本來下載csv文件。我正在使用python 3.6.1。下面的代碼Python urllib.error.HTTPError:HTTP錯誤403:禁止

from urllib import request 

demo_csv_url = 'http://www.sample-videos.com/csv/Sample-Spreadsheet-100-rows.csv' 

def downloadCSV(url): 
    response = request.urlopen(url) 
    csv = response.read() 
    csvStr = str(csv) 
    lines = csvStr.split('\\n') 
    dest = r'csv.csv' 
    fx = open(dest,"w") 
    for line in lines: 
     fx.write(line + '\n') 
    fx.close() 


downloadCSV(demo_csv_url) 

當我運行該腳本,我得到以下錯誤

Traceback (most recent call last): 
    File "C:\Users\Vivek\Desktop\py tutorials\download_csv.py", line 23, in <module> 
    downloadCSV(demo_csv_url) 
    File "C:\Users\Vivek\Desktop\py tutorials\download_csv.py", line 12, in downloadCSV 
    response = request.urlopen(url) 
    File "D:\softwares\installed softwares\python\lib\urllib\request.py", line 223, in urlopen 
    return opener.open(url, data, timeout) 
    File "D:\softwares\installed softwares\python\lib\urllib\request.py", line 532, in open 
    response = meth(req, response) 
    File "D:\softwares\installed softwares\python\lib\urllib\request.py", line 642, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "D:\softwares\installed softwares\python\lib\urllib\request.py", line 570, in error 
    return self._call_chain(*args) 
    File "D:\softwares\installed softwares\python\lib\urllib\request.py", line 504, in _call_chain 
    result = func(*args) 
    File "D:\softwares\installed softwares\python\lib\urllib\request.py", line 650, in http_error_default 
    raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 403: Forbidden 

我試圖增加更多的頭部像

hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 
     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 
     'Accept-Encoding': 'none', 
     'Accept-Language': 'en-US,en;q=0.8', 
     'Connection': 'keep-alive'} 

,然後打開URL作爲 response = request.urlopen(url,hdr) 但它會帶來更多的錯誤。你能不能讓我知道我在這裏做錯了什麼。 謝謝

+0

你可以做一個捲曲的URL嗎? – alDiablo

+0

https://stackoverflow.com/questions/41527592/urllib-error-httperror-http-error-403-forbidden – mars

回答

0

當您直接在瀏覽器中訪問它時,該URL會拋出403,所以它似乎按預期工作。如果你想捕捉403的使用try/except

如果內容受到Auth頭或Cookie的保護,您需要弄清楚這些內容並將其添加到請求中。

+0

謝謝布萊克。我從來沒有直接在瀏覽器中檢查網址。我嘗試了不同的網址,它工作正常。 –

0

您需要進行身份驗證才能訪問此數據,您需要提供某種「密碼」,「用戶名」。

enter image description here

+0

謝謝@oshaiken。我嘗試了一個不會拋出403錯誤並且工作正常的不同網址。 –

+0

以下是您未來參考的代碼列表https://en.wikipedia.org/wiki/List_of_HTTP_status_codes – oshaiken