You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Twitter API v2无locations参数,研究者能否获取自动GPS定位元数据?

问题

我希望从推文获取Twitter用户的GPS经纬度坐标,需要高精度地理位置,因此想要收集由Twitter通过GPS自动记录而非用户自行上报位置的推文。此前可通过tweepy.Stream类实现该功能,示例代码如下:

import tweepy

LOCATION = [-124.7771694, 24.520833, -66.947028, 49.384472]

class MyStreamListener(tweepy.Stream):
    # 自定义类方法
    def on_status(self, status):
        # 处理推文逻辑
        pass

stream = MyStreamListener(apikey, apikeysecret, accesstoken, accesstokensecret)
stream.filter(locations=LOCATION)

但根据tweepy文档,2022年4月29日及之后创建的新Twitter开发者应用无法使用Stream类,需改用Twitter API v2的StreamingClient。遗憾的是,StreamingClient的filter()方法及其他方法均未提供locations参数。这是否意味着Twitter不再向研究者提供此类元数据?

回答

Twitter API v2仍然提供高精度GPS位置元数据,只是获取和过滤方式与v1.1不同:

  • 高精度位置的标识
    设备GPS自动记录的高精度位置会存储在推文的geo.coordinates字段中,用户手动上报的位置则对应place字段。可以用has:geo规则筛选带有高精度GPS数据的推文。

  • StreamingClient的区域过滤方式
    StreamingClient没有直接的locations参数,但可以通过添加过滤规则实现区域筛选,使用bounding_box运算符,格式为bounding_box:[西 南 东 北],和你之前使用的LOCATION参数格式完全一致。

  • 示例代码
    以下是用Tweepy StreamingClient实现的完整示例:

    import tweepy
    
    # 自定义StreamingClient子类
    class MyStreamingClient(tweepy.StreamingClient):
        def on_tweet(self, tweet):
            # 处理包含高精度位置的推文
            if tweet.geo and tweet.geo.coordinates:
                print(f"高精度GPS坐标:{tweet.geo.coordinates}")
    
    # 初始化客户端(替换为你的Bearer Token)
    client = MyStreamingClient("your_bearer_token")
    
    # 添加过滤规则:指定区域+包含高精度GPS
    LOCATION = "-124.7771694 24.520833 -66.947028 49.384472"
    client.add_rules(tweepy.StreamRule(f"bounding_box:[{LOCATION}] has:geo"))
    
    # 启动流,需明确指定获取geo字段
    client.filter(tweet_fields=["geo"])
    
  • 注意事项

    • 需在Twitter开发者后台确保应用拥有Read Only或更高权限
    • 必须在filter()方法中指定tweet_fields=["geo"],否则接口不会返回位置数据

内容的提问来源于stack exchange,提问作者arilwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 18:06:19