1
votes

I would like to search Twitter for questions only tweets.

If I search in Twitter UI I get the correct results, but not using their API. Here is the code:

search_url = "https://api.twitter.com/2/tweets/search/recent"


# q = urllib.parse.quote('(Who OR What OR who OR where OR which)')
q = '(Who OR What OR who OR where OR which) ?'
query_params = {'query': q + ' -is:retweet 
(from:twitter)','max_results':'100','tweet.fields': 'text'}

If I include q = '?' only as I would do in UI it will result an error:

Exception: (400, '{"errors":[{"parameters":{"query":["%3F -is:retweet (from:twitter)"]},"message":"There were errors processing your request: no viable alternative at character \'%\' (at position 1)"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}')