是否可以在twitterStream.filter中添加几个复杂的条件?

问题描述 投票:0回答:1

此代码是抓取推文,其中包含一些关键字,并在新创建的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文件,只有空文件,即使它运行时它打印好推文的坐标。

这是下面的完整代码。


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"])
python twitter streaming
1个回答
0
投票

Twitter只允许在过滤器中添加一些参数:

  • 分隔
  • stall_warnings
  • filter_level
  • 语言
  • 跟随
  • 跟踪
  • 地点
  • 计数(已弃用)
  • 回复(已弃用)
  • stringify_friend_id(不建议使用)

https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/basic-stream-parameters

© www.soinside.com 2019 - 2024. All rights reserved.