Reputation: 23
I'm trying to collect tweet data which has specific hashtag by using twitter api.
I use the python code below.
https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.py
It does work seemingly.
However, when I change the line 14 from
query = "from:twitterdev -is:retweet"
to
query = "#apple"
it does not work anymore. Error message is below.
Exception: (400, '{"errors":[{"parameters":{"query":[""]},"message":"Invalid \'query\':\'\'. \'query\' must be a non-empty string"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}')
It seems to relate #. How can I fix this?
Upvotes: 1
Views: 2083
Reputation: 66
try:
query = "%23apple"
In Twitter's sample code, the variable query
is used as a parameter of the URL. Certain characters are not allowed in the parameter of an URL (like /
, ?
, #
etc.).
The URL https://api.twitter.com/2/tweets/search/recent?query=#apple
is invalid.
%23
is the encoded value of #. The URL will look like this: https://api.twitter.com/2/tweets/search/recent?query=%23apple
.
The Twitter API then can %23
translate back into #
.
Here is a in-depth explanation: https://www.w3schools.com/tags/ref_urlencode.ASP
Upvotes: 3
Reputation: 332
It's hard to troubleshoot what might be going on here. A couple of suggestions.
.json()
from response.json()
and run the code again. Once you've done that, you can troubleshoot your code in the interactive command-line very of Python.To be clear, go to your terminal, enter Python, and then load your functions one by one (Except main()
). Then go through the code in main()
code one line at a time. After you run json_response = connect_to_endpoint(url, headers)
you should still get an error, however, you can then use the objects in the response.request
object to see what is going on. For example, after doing the above and then running...
response = connect_to_endpoint(url, headers)
...you can then execute response.request.url
to print the full URL that was sent to Twitter and compare it to the form of that found in the Building queries page linked above.
See the second answer here for a nice list of what is available. You can also (after running response = connect_to_endpoint(url, headers)
) simply type response.request.
(include both periods) into your terminal and then press the shift
key a couple of times to make your terminal print these options.
Upvotes: 0