开发者

How do I fix a "JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)"?

开发者 https://www.devze.com 2023-01-09 19:42 出处:网络
I\'m trying to get Twitter API search results for a given hashtag using Python, but I\'m having trouble with this \"No JSON object could be decoded\" error. I had to add the extra % towards the end of

I'm trying to get Twitter API search results for a given hashtag using Python, but I'm having trouble with this "No JSON object could be decoded" error. I had to add the extra % towards the end of the URL to prevent a string formatt开发者_如何学Pythoning error. Could this JSON error be related to the extra %, or is it caused by something else? Any suggestions would be much appreciated.

A snippet:

import simplejson
import urllib2

def search_twitter(quoted_search_term): 
    url = "http://search.twitter.com/search.json?callback=twitterSearch&q=%%23%s" % quoted_search_term
    f = urllib2.urlopen(url)
    json = simplejson.load(f)
    return json


There were a couple problems with your initial code. First you never read in the content from twitter, just opened the url. Second in the url you set a callback (twitterSearch). What a call back does is wrap the returned json in a function call so in this case it would have been twitterSearch(). This is useful if you want a special function to handle the returned results.

import simplejson
import urllib2

def search_twitter(quoted_search_term): 
    url = "http://search.twitter.com/search.json?&q=%%23%s" % quoted_search_term
    f = urllib2.urlopen(url)
    content = f.read()
    json = simplejson.loads(content)
    return json
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号