我正在开发一个 django 应用程序,它从 facebook 图形 api 获取数据并绘制它的网络图。该应用程序在本地运行良好,但是当我在 pythonanywhere 上托管它时,它给了我一个错误 No JSON object could be decoded 。另外,作为 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%

最佳答案

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

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

关于python - 无法解码 JSON 对象 - django - pythonanywhere,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22880762/

10-12 02:48