tweepy 查询中的逻辑运算符

Logical operators in tweepy query

我正在使用如下命令和查询在历史完整存档中查找推文:

data = []
for tweet in tweepy.Cursor(api.search_full_archive, label = 'Understanding', query = '((PAN AND Nuevo León) OR (Partido Accion Nacional AND Nuevo León) OR (PAN AND Monterrey) OR (Partido Acción Nacional AND Monterrey))', maxResults = 100, fromDate = '202105060000', toDate = '202107060000').items(10):
    data.append(tweet._json)  

Then I get the error: HTTPException: 422 Unprocessable Entity There were errors processing your request: Reference to invalid operator 'AND'. For logical AND, use a single space ' ' between clauses (at position 7), Reference to invalid operator 'AND'. For logical AND, use a single space ' ' between clauses (at position 51), Reference to invalid operator 'AND'. For logical AND, use a single space ' ' between clauses (at position 75), Rule length exceeds the max allowable. The max is 128 and this rule is 132. Rule text is '((PAN AND Nuevo León) OR (Partido Accion Nacional AND Nuevo León) OR (PAN AND Monterrey) OR (Partido Acción Nacional AND Monterrey))', Reference to invalid operator 'AND'. For logical AND, use a single space ' ' between clauses (at position 118)

问题归结为,例如,如果我有四个(字符串)子句 'hi'、'goodbye'、'hello' 和 'bye' 并且希望查询是 ('hi' OR 'hello') AND ('goodbye' OR 'bye') 语法必须如何?

您的答案在错误消息中。您不需要 ANDs。正如它所说:

For logical AND, use a single space ' '

Twitter docs 也支持这一点:

Operator: Finds Tweets...

"watching now": containing both “watching” and “now”. This is the default operator.

所以你不需要指定AND,它是由单个space隐含的。您的查询也太长,删除所有 AND 会将查询缩短为 116 个字符:

((PAN Nuevo León) OR (Partido Accion Nacional Nuevo León) OR (PAN Monterrey) OR (Partido Acción Nacional Monterrey))

对于您最后给出的另一个示例,查询将只是:

('hi' OR 'hello') ('goodbye' OR 'bye')

由于单个 space ' ',当未用双引号括起来时 "" 是隐式的 AND.