无法使用chrome中的urllib2打开URL链接

时间:2014-06-08 05:24:17

标签: python-2.7 urllib2

我只需在Chrome浏览器中输入网址链接即可打开网页 但是,当我将此URL链接移动到下面的代码时,它将提示我错误消息:

CODE:

import urllib2

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

ERROR:

  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链接更改为其他链接地址,它确实有效。 网站是否设置限制或我应该注意什么?

1 个答案:

答案 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()