【问题标题】:Collect tweets from a certain country收集来自某个国家的推文
【发布时间】:2019-05-31 21:41:18
【问题描述】:

我正在尝试运行 Twitter Streaming API 以仅收集来自某个国家/地区的推文。

尝试将长/纬度坐标写为过滤器会出现语法错误:

位置参数跟在关键字参数之后

我愿意使用地理标签按国家或城市进行过滤,但不知道需要在代码中写入什么内容。

仅从大致位置流式传输推文的解决方案会很棒。

# Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Variables that contain the user credentials to access Twitter API
access_token = "put your keys here"
access_token_secret = "puts your keys here"
consumer_key = "puts your keys here"
consumer_secret = "puts your keys here"


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        print(data)
        return True

    def on_error(self, status):
        print(status)


if __name__ == '__main__':

    #This handles Twitter authentification and the connection to Twitter 
    Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

【问题讨论】:

  • 您以后可能需要重新生成您的 Twitter API 密钥。
  • 感谢您的提醒。不过这些都是旧钥匙。

标签: python-3.x twitter location


【解决方案1】:

您需要提供您所在位置的经纬度边界框。像这样的,

stream.filter(locations=[-74,40,-73,41])

这是 Twitter 的documentation

【讨论】:

  • 这正是我尝试导致“位置参数跟随关键字参数”错误的方法。解决方案?
  • 我认为你把它从你的代码 sn-p 中遗漏了,所以很难知道你做错了什么。您的坐标顺序可能有误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 2011-01-01
  • 2013-08-02
相关资源
最近更新 更多