我是python编程和Twitter API的新手。 我厌倦了从特定时间段(例如11/24 / 216-11 / 27/2017)收集带有标签的推文,我的目标是从那些提取的推文中获取坐标并将坐标和推文文本保存到csv文件中。 但我的问题是我不知道如何设置时间过滤器并将它们保存到文件中。更重要的是,只有少数推文包含坐标,这是常见的吗? 以下是我在网上找到的python脚本。
import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
#Enter Twitter API Key information
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
file = open("C:\\Output.csv", "w") #This script didn't work on my Mac#
强文 file.write( “X,Y \ n” 个)
data_list = []
count = 0
class listener(StreamListener):
def on_data(self, data):
global count
#How many tweets you want to find, could change to time based
if count <= 2000:
json_data = json.loads(data)
coords = json_data["coordinates"]
if coords is not None:
print coords["coordinates"]
lon = coords["coordinates"][0]
lat = coords["coordinates"][1]
data_list.append(json_data)
file.write(str(lon) + ",")
file.write(str(lat) + "\n")
count += 1
return True
else:
file.close()
return False
def on_error(self, status):
print status
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
#What you want to search for here
twitterStream.filter(track=[""])