此代码正在爬网包含某些关键字的推文,并将这些推文的坐标写入新创建的csv文件中。 是否可以通过修改“ twitterStream.filter(track = [” food“])”“添加如下所示的多个条件?
keywords = ‘food'
limit = 1000
near = 'Detroit MI'
within = '15km'
since = '2018-06-01'
until = '2018-06-05'
lang = ‘en'
我知道filterquery可以包含多个条件,但是我不确定它还可以包含复杂的条件,例如地点,开始日期或结束日期。我也想知道是否可以在python上使用filterquery。 除了这段代码,创建csv文件只不过是空文件,即使它运行时也能很好地打印tweets的坐标。
这是下面的完整代码。
import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
consumer_key=".."
consumer_secret=".."
access_token=".."
access_secret=".."
file = open("result.csv", "w")
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())
twitterStream.filter(track=["food"])
答案 0 :(得分:0)
Twitter仅允许在过滤器中添加一些参数:
https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/basic-stream-parameters