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

使用Tweepy查询Twitter列表用户推文时list操作符报错,求解决方案

问题描述

我用Tweepy创建了Twitter列表并添加用户,尝试筛选该列表用户的推文时出现错误。

创建列表的代码:

import tweepy

# name of the list
name = "tweepy_list"

client = tweepy.Client(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret)
l = client.create_list(name)
id_l = l[0]['id']

其中id_l为该列表的ID。

向列表添加用户的代码:

client.add_list_member(id_l, user_id)

筛选推文的代码(报错部分):

query = "{0} lang:es -is:retweet list:{1}:" .format(topic, id_l)
tweets = client.search_recent_tweets(query=query,
                                     tweet_fields = ["created_at", "text", "source"],
                                     user_fields = ["name", "username", "location", "verified", "description"],
                                     max_results = 10,
                                     expansions='author_id',
                                     user_auth=True
                                     )

报错信息:

BadRequest: 400 Bad Request
There were errors processing your request: Reference to invalid operator 'list'. Operator is not available in current product or product packaging. Please refer to complete available operator list at https://developer.twitter.com/en/docs/twitter-api/enterprise/rules-and-filtering/operators-by-product

请问Tweepy或其他Python包是否支持这类查询需求?


解决方案

首先明确:list:操作符属于Twitter API的企业级功能,免费/基础版API无法使用,这是报错的核心原因。针对你的需求,有两种可行的替代方案:

方案一:获取列表成员ID,批量筛选推文

  1. 先拉取目标列表的所有成员ID:
# 分页获取列表所有成员
members = []
pagination_token = None
while True:
    response = client.get_list_members(id_l, pagination_token=pagination_token)
    members.extend(response.data)
    pagination_token = response.meta.get('next_token')
    if not pagination_token:
        break
member_ids = [member.id for member in members]
  1. 用from:操作符批量指定用户ID(最多支持100个ID),构建查询语句:
# 拼接用户ID的查询条件
from_clause = " OR ".join([f"from:{mid}" for mid in member_ids])
# 组合完整查询
query = f"{topic} lang:es -is:retweet ({from_clause})"
# 执行搜索
tweets = client.search_recent_tweets(query=query,
                                     tweet_fields=["created_at", "text", "source"],
                                     user_fields=["name", "username", "location", "verified", "description"],
                                     max_results=10,
                                     expansions='author_id',
                                     user_auth=True
                                     )

如果列表成员超过100个,需要分批次构建查询并合并结果。

方案二:直接调用列表推文接口

Tweepy封装了官方的get_list_tweets接口,可直接获取列表内成员的推文,无需手动构建复杂查询:

# 分页获取列表内的推文
tweets = []
pagination_token = None
while True:
    response = client.get_list_tweets(id_l,
                                     tweet_fields=["created_at", "text", "source"],
                                     user_fields=["name", "username", "location", "verified", "description"],
                                     max_results=100,
                                     expansions='author_id',
                                     exclude=['retweets'],  # 排除转发
                                     lang='es',  # 指定语言
                                     pagination_token=pagination_token
                                     )
    tweets.extend(response.data)
    pagination_token = response.meta.get('next_token')
    if not pagination_token:
        break

关于其他Python包

其他Twitter API的Python封装包(如python-twitter)本质都是调用官方API,核心限制和Tweepy一致——免费版无法使用list:操作符,只能通过上述两种方案实现需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 21:57:19