尝试使用urllib.request访问网站时出现HTTP错误503

时间:2018-11-04 13:03:15

标签: python-3.x urllib http-error

我正在尝试为网站创建API。当我为某个 lewd 网站编写API时,我遇到了类似的问题。我通过添加以下代码对其进行了修复:

        urlheader = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46'}

        request = Request(self.webaddr, headers = urlheader)

因此,我尝试将代码添加到新API中。它仍然给出错误,所以我尝试了不同的标题。仍然没有成功。这是它给的错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Personal Shit\Python\osu.py\osu.py", line 13, in __init__
    self.page = urlopen(request).read().decode('utf-8')
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 503: Service Temporarily Unavailable

我写的代码是这样的:

def __init__(self, username_id):
        self.webaddr = 'http://osu.ppy.sh/users/' + quote(username_id)
        urlheader = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'}

        request = Request(self.webaddr, headers = urlheader)

        self.page = urlopen(request).read().decode('utf-8')

我已尽力解决此问题。有什么建议吗?

0 个答案:

没有答案