twitter - Python CGI script returning inconsistent results -
so have python cgi script running on apache server. basically, webpage, user enters word form, , word passed script. word used query twitter search api , return tweets word. issue is, i'm running query in loop 3 pages of results returned (approximately 300 tweets). call script (which prints out tweets html page), page display 5 tweets, 18, completley random numbers. timeout issue, or missing basic in code? python cgi script posted below, in advance.
#!/usr/bin/python # import modules cgi handling import cgi, cgitb import urllib import json # create instance of fieldstorage form = cgi.fieldstorage() # data fields topic = form.getvalue('topic') results=[] x in range(1,3): response = urllib.urlopen("http://search.twitter.com/search.json?q="+topic+"&rpp=100&include_entities=true&result_type=mixed&lang=en&page="+str(x)) pyresponse= json.load(response) results= results + pyresponse["results"] print "content-type:text/html\r\n\r\n" print "<!doctype html>" print "<html>" print "<html lang=\"en\">" print "<head>" print "<meta charset=\"utf-8\" />" print "<meta name=\"description\" content=\"\"/>" print "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>" print "<title>data analysis %s </title>" %(topic) print "</head>" print "<body>" print "<label>" in range(len(results)): print str(i)+": "+results[i]["text"]+ "<br></br>" print "</label>" print "</body>" print "</html>"
first of point out range(1,3)
not 3 pages expecting.
however, running python code in interpreter encountered exception @ point:
>>> in range(len(results)): ... print str(i) + ": "+ results[x]["text"] <a few results print successfully> unicodeencodeerror: 'latin-1' codec can't encode character u'\u0001f611' in position 121: ordinal not in range(256)
modifying encoding print them all:
>>> in range(len(results)): ... print str(i) + ": "+ results[i]["text"].encode('utf-8') <success>
Comments
Post a Comment