我正在开发一个django应用程序,它从facebook图形api中获取数据并绘制它的网络图。该应用在本地运行良好,但当我在pythonanywhere上托管时,它会给我一个错误No JSON object could be decoded
。另外,作为pythonany的新手,我不知道如何在控制台上打印错误。
以下是代码:
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&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%
答案 0 :(得分:3)
某些Python网址库与我们用来过滤免费帐户互联网访问权限的squid代理之间存在不兼容。
尝试使用请求(我相信错误已修复)或urllib3而不是urllib。
答案 1 :(得分:2)
小学,您无法通过Squid配置访问Facebook,而是获得JSON流,您将获得HTML Squid错误页面。这就是全部。
答案 2 :(得分:0)
这似乎是limitation of the basic PythonAnywhere accounts:
我怀疑你无法通过HTTPS进行POST。通过代理服务器连接时存在很多限制。错误消息来自代理,似乎很清楚。
黑客帐户拥有无限制的互联网访问权限,根本无法通过代理服务。
...
除非您有付费帐户,否则无法绕过代理,这是正确的。您可以升级您的帐户并进行测试而不会产生任何风险 - 如果您发现它无法正常工作,并且您在30天内再次降级,则可以要求全额退还第一个月的费用。