Home > Back-end >  Logical operators in tweepy query
Logical operators in tweepy query

Time:10-23

I'm looking for the tweets in the historical full archieve with a command and query like this:

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)

The question boils down to, foe example, if I have four (string) clauses 'hi', 'goodbye', 'hello' and 'bye' and want the query to be ('hi' OR 'hello') AND ('goodbye' OR 'bye') how the syntax must be?

CodePudding user response:

Your answer is in the error message. You don't need the ANDs. As it says:

For logical AND, use a single space ' '

The Twitter docs also back this up:

Operator: Finds Tweets...

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

So you don't need to specify AND, it is implied by the single space. Your query was also too long, removing all the ANDs shortens the query to 116 characters:

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

With the other example you gave at the end, the query would simply be:

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

Since the single space ' ', when not encapsulated in double quotes "" is an implicit AND.

  • Related