通过存储100个用户,然后请求信息(获取用户/查找),然后将其与其他信息(python)放在一起,从而减少twitter api负载
我知道long titel——我想不出其他任何东西 因此,我正在编写一个python脚本,它将Twitter搜索API的元素保存到一个csv文件中通过存储100个用户,然后请求信息(获取用户/查找),然后将其与其他信息(python)放在一起,从而减少twitter api负载,twitter,python,Twitter,Python,我知道long titel——我想不出其他任何东西 因此,我正在编写一个python脚本,它将Twitter搜索API的元素保存到一个csv文件中 writer = csv.writer(open('stocks.csv', 'a', buffering=0)) writer.writerows([(screen_name, hashtags, expanded_url , coordinates , geo , in_reply_to_user_id, followers)]) 但我想加上推
writer = csv.writer(open('stocks.csv', 'a', buffering=0))
writer.writerows([(screen_name, hashtags, expanded_url , coordinates , geo , in_reply_to_user_id, followers)])
但我想加上推特用户有多少追随者
现在这是通过GetUsers/lookup Twitter API完成的,该API限制为每小时350个请求,但允许同时查找多达100个用户
现在,我的脚本在查找tweet时会查找用户的关注者,并将tweet的所有信息传递到csv文件中
这很好,但经过350次搜索后,我达到了极限
现在我的问题是:
我是否可以让脚本搜索100次并将这100个用户名存储在某个位置,一旦达到100,它将调用GET users/lookup并将搜索信息的信息右侧插入excel文件:
Excel示例:
[info from search ...(in many columns)] [followers of the user who sent the tweet]
[info from search ...(in many columns)] [followers of the user who sent the tweet]
[info from search ...(in many columns)] [followers of the user who sent the tweet]
根据要求:
import urllib2
import urllib
import json
import time
s = u'@apple OR @iphone OR @aapl OR @imac OR @ipad OR @mac OR @macbook OR macbook OR mac OR ipad OR iphone 4s OR iphone 5 OR @iphone4s OR @ iphone 5 OR aapl OR iphone'
info = urllib2.quote(s.encode("utf8"))
page = "?q="
openurl = urllib.urlopen("http://search.twitter.com/search.json"+ page + info)
quota = 150
user = 'twitter'
user_info = urllib.urlopen("https://api.twitter.com/1/users/lookup.json?screen_name="+user)
while quota > 10:
openurl2 = urllib.urlopen("https://api.twitter.com/1/account/rate_limit_status.json")
twitter_quota = openurl2.read()
quota_json = json.loads(twitter_quota)
quota = quota_json['remaining_hits']
twitter_search = openurl.read()
table_search = json.loads(twitter_search)
print table_search
print str(table_search[u'results'][1][u'iso_language_code'])
lines = 0
linesmax = len(table_search[u'results'])
print linesmax
while lines < linesmax:
table_timeline_inner = table_search[u'results'][lines]
next = table_search[u'next_page']
lang = table_timeline_inner[u'iso_language_code']
to = table_timeline_inner[u'to_user_name']
text = table_timeline_inner[u'text']
user = table_timeline_inner[u'from_user']
geo = table_timeline_inner[u'geo']
time = table_timeline_inner[u'created_at']
result_type = table_timeline_inner[u'metadata'][u'result_type']
id = table_timeline_inner[u'id']
对。不要一拿到每一行就写它,而是将它存储在一个临时集合中。当这个集合有100个元素时,使用一个请求查找所有元素。然后迭代集合中的每个元素,并将其与从请求中获得的数据一起写入,这些数据必须通过userid或其他方式进行匹配
我不会为您编写代码…如果您发布到目前为止的所有代码,而不仅仅是将变量写入CSV文件的那两行代码,我会提供帮助。您知道,您可以使用writerow,而不是使用writerow和一个元素列表。顺便说一句,有一个Python Twitter库