while reading json data from url using python gives error "urllib.error.HTTPError: HTTP Error 403: Forbidden"

while reading json data from url using python gives error "urllib.error.HTTPError: HTTP Error 403: Forbidden"

使用此代码,我正在阅读 URL 并使用数据进行过滤,但 urllib 无法工作

url = "myurl"
response = urllib.request.urlopen(url)
data = json.loads(response.read())

昨天运行良好,但现在出现错误:

Traceback (most recent call last):
  File "vaccine_survey.py", line 22, in <module>
    response = urllib.request.urlopen(url)
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/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

这对我有用,这里 'myurl' 是一个 url 地址

from urllib.request import Request, urlopen

req = Request('myurl', headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(req).read()
data = json.loads(response.read())