2016-07-28 58 views
1

HTTP錯誤403:通過使用以下兩個命令中的一個來生成禁止。python 3:使用請求時收到403:禁止錯誤

requests.get('http://www.allareacodes.com')

urllib.request.urlopen('http://www.allareacodes.com')

但是,我能夠瀏覽這個網站在Chrome和檢查其來源。此外,我的cygwin中的wget也能抓取html源碼。

任何人都知道如何通過在python中使用包來獲取本網站的源代碼?

回答

1

您的代碼中存在錯誤請求。它應該是:

import requests 
r = requests.get('http://www.allareacodes.com') 
print(r.text) 

在你的情況然而,網站上有一個「NOINDEX」文件,該文件從獲取原始的HTML數據停止腳本。作爲解決方案,只需僞造你的標題,以便網站認爲你是一個真正的用戶。

例子:

import requests 
r = requests.get('http://www.allareacodes.com', headers={ 
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" 
}) 
print(r.text) 
+0

非常感謝!我將檢查文檔的標題選項。 – user6651227