3

我正在开发一个 django 应用程序,它从 facebook graph api 获取数据并绘制它的网络图。该应用程序在本地运行良好,但是No JSON object could be decoded当我在pythonanywhere上托管它时出现错误。另外,作为 pythonanywhere 的新手,我不知道如何在控制台上打印错误。

这是代码:

import urllib,json
from django.shortcuts import render
from main.forms import InputForm
from django.http import HttpResponseRedirect

def main(request):
    if request.method == 'POST': # If the form has been submitted
        input_form = InputForm(request.POST)
        if input_form.is_valid():
            cd = input_form.cleaned_data
            actoken = cd['access_token']
            keyword = cd['keyword']
            url = "https://graph.facebook.com/search?q="+keyword+"&type=post&fields=likes.fields(name).limit(10),comments,message&limit=5&access_token="+actoken
            fetch = urllib.urlopen(url).read()
            print fetch # don't know the response
            data = json.loads(fetch)
            return HttpResponseRedirect('/graph/')
        else:
            input_form = InputForm(request.POST)
            return render(request, 'index.html', {
                'input_form': input_form,
            })
    else:
        input_form = InputForm()
        return render(request, 'index.html', {
            'input_form': input_form,
        })

这是堆栈跟踪:

追溯:

File "/home/architv/.virtualenvs/django16/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/architv/fb-network-graph/fbnetworkgraph/main/views.py" in main
  15.             data = simplejson.loads(fetch)
File "/usr/lib/python2.7/json/__init__.py" in loads
  338.         return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py" in decode
  365.         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py" in raw_decode
  383.             raise ValueError("No JSON object could be decoded")

Exception Type: ValueError at /main/
Exception Value: No JSON object could be decoded

编辑: 我按照 glenn 的建议浏览了服务器日志文件,发现了这个:

2014-04-05 09:22:22 https://graph.facebook.com/search?q=ukraine&type=post&fields=likes.fields(name).limit(10),comments,message&limit=5&access_token=CAACEdEose0cBAIhZBJSXHTHOoGZCFZC2xbPDNuNKX9NIksZBMQudtzN2A7o4op5ZBkOhYTZAS6JFcn5lQMNY94nhntZAhmA4DZAWuVg0wqHVHI3VQToq9yhHeQZBL75BgaPMNIbUj9XKQ7ICKJeM3R5YAShXjBxPZAcmbfmxcARFQZA0UjartHP3lWpWCR2wRjJ8WwZD
2014-04-05 09:22:22 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ERROR: The requested URL could not be retrieved</title> <style type="text/css"><!--   /*#012 Stylesheet for Squid Error pages#012 Adapted from design by Free CSS Templates#012 http://www.freecsstemplates.org#012 Released for free under a Creative Commons Attribution 2.5 License#012*/#012#012/* Page basics */#012* {#012#011font-family: verdana, sans-serif;#012}#012#012html body {#012#011margin: 0;#012#011padding: 0;#012#011background: #efefef;#012#011font-size: 12px;#012#011color: #1e1e1e;#012}#012#012/* Page displayed title area */#012#titles {#012#011margin-left: 15px;#012#011padding: 10px;#012#011padding-left: 100px;#012#011background: url('http://www.squid-cache.org/Artwork/SN.png') no-repeat left;#012}#012#012/* initial title */#012#titles h1 {#012#011color: #000000;#012}#012#titles h2 {#012#011color: #000000;#012}#012#012/* special event: FTP success page titles */#012#titles ftpsuccess {#012#011backgro
2014-04-05 09:22:22   body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; }  --></style> </head><body id=ERR_UNSUP_REQ> <div id="titles"> <h1>ERROR</h1> <h2>The requested URL could not be retrieved</h2> </div> <hr>  <div id="content"> <p>The following error was encountered while trying to retrieve the URL: <a href="https://graph.facebook.com/search?">https://graph.facebook.com/search?</a></p>  <blockquote id="error"> <p><b>Unsupported Request Method and Protocol</b></p> </blockquote>  <p>Squid does not support all request methods for all access protocols. For example, you can not POST a Gopher request.</p>  <p>Your cache administrator is <a href="mailto:webmaster?subject=CacheErrorInfo%20-%20ERR_UNSUP_REQ&amp;body=CacheHost%3A%20glenn-liveproxy1%0D%0AErrPage%3A%20ERR_UNSUP_REQ%0D%0AErr%3A%20%5Bnone%5D%0D%0ATimeStamp%3A%20Sat,%2005%20Apr%202014%2009%3A21%3A47%20GMT%0D%0A%0D%
4

3 回答 3

3

一些 Python url 库与我们用来过滤免费帐户 Internet 访问的 squid 代理之间存在不兼容。

尝试使用请求(我相信该错误已修复)或 urllib3 而不是 urllib。

于 2014-04-05T15:51:49.817 回答
2

初级,您无法通过 Squid 配置访问 Facebook,而是要获取 JSON 流,您会得到一个 HTML Squid 错误页面。就这样。

于 2014-04-05T14:26:14.277 回答
0

这似乎是基本 PythonAnywhere 帐户的限制

我怀疑您不能通过 HTTPS 进行 POST。通过代理服务器连接时有很多限制。错误消息来自代理,看起来很清楚。

黑客帐户具有不受限制的互联网访问权限,根本不通过代理服务。

...

除非您有付费帐户,否则无法绕过代理,这是正确的。您可以毫无风险地升级您的帐户并对其进行测试 - 如果您发现它不起作用并且您在 30 天内再次降级,您可以要求全额退款第一个月的费用。

于 2014-04-05T15:27:55.803 回答