A python beginner here, trying to get Twitter cursoring working in my script so I might iterate all of the users that belong to a list on Twitter. Pretty simple logic here. Start with this API request:
https://api.twitter.com/1/lists/members.json?slug=all-fox-news&owner_screen_name=foxnews&cursor=-1
Then have a for loop alter the cursor =-1 to whatever the next_cursor_str is in parsed JSON. However, I'm having a hard time storing the next_cursor_str as a string. Has anyone had experience with this? Below is my code, works fine, just no cursor loop:
import urllib2import jsonimport csvfrom time import sleepoutfile_path='Out.csv'writer = csv.writer(open(outfile_path, 'w'))headers = ['users']writer.writerow(headers)url = urllib2.Request('https://api.twitter.com/1/lists/members.json?slug=all-fox-news&owner_screen_name=foxnews&cursor=-1')parsed_json = json.load(urllib2.urlopen(url))print parsed_jsonfor tweet in parsed_json['users']: row = [] row.append(str(tweet['screen_name'].encode('utf-8'))) writer.writerow(row)sleep(5)
Per the answer below parsed_json["next_cursor_str"] is exactly what I need. I thought a while loop would be good here, but yet it fails to end on 0:
n = parsed_json["next_cursor_str"]int(n)while n is not 0: url = urllib2.Request('https://api.twitter.com/1/lists/members.json?slug=all-fox-news&owner_screen_name=foxnews&cursor='+ str(n)) parsed_json = json.load(urllib2.urlopen(url)) print parsed_json for tweet in parsed_json['users']: row = [] row.append(str(tweet['screen_name'].encode('utf-8'))) writer.writerow(row) n = parsed_json["next_cursor_str"]