本文介绍了从 pythonanywhere 打开 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This code works well on my local machine, but when I upload and run it on pythonanywhere.com it gives me this error.
My Code:

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

P.S. i'm working on python 2.7

推荐答案

PythonAnywhere 上的免费帐户仅限于 白名单 站点,仅限 http/https,并且通过代理访问.这里有更多信息:

Free accounts on PythonAnywhere are restricted to a whitelist of sites, http/https only, and access goes via a proxy. There's more info here:

PythonAnywhere wiki:为什么我在打开 url 时会收到 403 禁止错误?"

这篇关于从 pythonanywhere 打开 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 00:03