我给我的代码,如下面的MWE,它工作得很好.

import tweepy

BEARER = "my-bearer-token"

class MyListener(tweepy.StreamingClient):
    def on_data(self, data):
        try:
            with open('python.json', 'a') as f:
                f.write(str(data))
                print(data)
                return True

        except BaseException as e:
            print("Error on_data: %s" % str(e))

    def on_connect(self):
        print('Connected..!')
        
    def on_error(self, status):
        print(status)
        return True

我定义了以下规则来获取特定于位置的数据:

stream = MyListener(BEARER)
stream.add_rules(tweepy.StreamRule('place:Lagos OR place:Abuja has:geo'))
stream.filter()

并成功检索到以下数据(来自print(data)语句):

$ python tweetify.py
Connected..!
b'{"data":{"id":"1559694255035138048","text":"@halimahthebird N*gga the next day "},"matching_rules":[{"id":"1559694230766895104","tag":""}]}'
b'{"data":{"id":"1559694261976764421","text":"@O_luwatomiwa Mental illness I tell you \xf0\x9f\x98\x82"},"matching_rules":[{"id":"1559694230766895104","tag":""}]}'
b'{"data":{"id":"1559694339709730816","text":"@elonmusk He gon buy united at the dip"},"matching_rules":[{"id":"1559694230766895104","tag":""}]}'
...

很好,过滤器起作用了,这些推文来自规则中定义的位置.

*Question

但我想要的是位置坐标[LONG, LAT],而不是推文.我添加了运算符has:geo,但不确定如何检索坐标.

推荐答案

您需要设置代码来请求所需的字段,如下所示:

class MyListener(tweepy.StreamingClient):
    def on_data(self, data):
      data_obj = json.loads(data.decode('utf8'))
      print(json.dumps(data_obj,indent=2))
      return True

    def on_connect(self):
      print('Connected..!')
        
    def on_error(self, status):
      print(status)
      return True


stream = MyListener(BEARER)
stream.add_rules(tweepy.StreamRule('place:London OR place:Paris has:geo', tag="london-paris"))
stream.filter(tweet_fields=["geo","created_at","author_id"],place_fields=["id","geo","name","country_code","place_type","full_name","country"],expansions=["geo.place_id"])

最新/最新信息将在includes.places.geo.bbox对象中.

Python相关问答推荐

根据二元组列表在pandas中创建新列

Django REST Framework:无法正确地将值注释到多对多模型,不断得到错误字段名称字段对模型无效'<><>

如何根据一列的值有条件地 Select 前N组?

如何在TensorFlow中分类多个类

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

使用Python查找、替换和调整PDF中的图像'

如何杀死一个进程,我的Python可执行文件以sudo启动?

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

Js的查询结果可以在PC Chrome上显示,但不能在Android Chrome、OPERA和EDGE上显示,而两者都可以在Firefox上运行

有了Gekko,可以创建子模型或将模型合并在一起吗?

PYTHON中的selenium不会打开 chromium URL

有没有一种方法可以根据不同索引集的数组从2D数组的对称子矩阵高效地构造3D数组?

将鼠标悬停在海运`pairplot`的批注/高亮显示上

Pandas查找给定时间戳之前的最后一个值

是否从Python调用SHGetKnownFolderPath?

SQL模型中包含日期时间的TypeError

如何在函数签名中输入数据类字段

nameError_C未定义

有条件的滚动平均数(面试问题)

如果init被重载,如何输入提示一个基于init的函数的返回类型