python urlopen返回错误

时间:2018-05-17 15:11:44

标签: python urlopen

我正在尝试解析来自' https://datausa.io/profile/geo/jacksonville-fl/#intro'的一些数据,但我不确定如何从python访问它。我的代码是:

adress, headers = urllib.request.urlretrieve('  https://datausa.io/profile/geo/jacksonville-fl/#intro')
handle = open(adress)

并返回错误:

Traceback (most recent call last):
  File "C:/Users/Jared/AppData/Local/Programs/Python/Python36-32/capstone1.py", line 16, in <module>
    adress, headers = urllib.request.urlretrieve('  https://datausa.io/profile/geo/jacksonville-fl/#intro')
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\Jared\AppData\Local\Programs\Python\Python36-32\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

请解释有什么问题或告诉我更好的方式来访问该页面。此外,&#39; .io&#39;后缀affecthow python处理它? 感谢。

1 个答案:

答案 0 :(得分:0)

这对我有用:

import requests
url = "https://datausa.io/profile/geo/jacksonville-fl/#intro"
req = requests.request("GET",url)