gpt4 book ai didi

python - 从 pythonanywhere 打开 url

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:09 24 4
gpt4 key购买 nike

这段代码在我的本地机器上运行良好,但是当我在 pythonanywhere.com 上上传并运行它时,它会给我这个错误。
我的代码:

url = "http://www.codeforces.com/api/contest.list?gym=false"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(url, headers=hdr)
opener = urllib2.build_opener()
openedReq = opener.open(req, timeout=300)

错误:

Traceback (most recent call last):
File "/home/GehadAbdallah/main.py", line 135, in openApi
openedReq = opener.open(req, timeout=300)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

附言我正在研究 python 2.7

最佳答案

PythonAnywhere 上的免费帐户仅限于 whitelist网站,仅 http/https,访问通过代理进行。这里有更多信息:

PythonAnywhere wiki: "why do I get a 403 forbidden error when opening a url?"

关于python - 从 pythonanywhere 打开 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com