【发布时间】: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