2014-06-08 59 views
0

只需在我的Chrome瀏覽器中輸入URL鏈接即可打開網頁 但是,當我將此URL鏈接移動到下面的代碼時,它會提示我錯誤消息:無法在chrome中使用urllib2打開URL鏈接

CODE:

import urllib2 

url = 'http://www.klse.info/companies/listed-companies/alphabet/A' 
page = urllib2.urlopen(url).read() 

錯誤:

File "C:\Python27\lib\urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 410, in open 
    response = meth(req, response) 
    File "C:\Python27\lib\urllib2.py", line 523, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python27\lib\urllib2.py", line 448, in error 
    return self._call_chain(*args) 
    File "C:\Python27\lib\urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 531, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
HTTPError: HTTP Error 403: Forbidden 

任何人有任何的想法與此? 我試圖改變URL鏈接到其他鏈接地址,它確實有效。 網站是否設置了限制或任何我應該注意的事項?

+0

我看了一些線程,多說,限制是來自谷歌。我可否知道是否有任何解決方法可以解決這個問題? –

回答

0

如何擺脫HTTP錯誤403:禁止?

請參考下面的代碼...

url = 'http://www.klse.info/companies/listed-companies/alphabet/A' 
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"}) 
page = urllib2.urlopen(req).read()