Searching tweets

In the previous script, we can add a search() function to search for and retrieve tweets from a specific search parameter. In this example, we are using the 'q' parameter with the 'python' value as search term. For this task, we can use the search endpoint at https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html. The endpoint (https://api.twitter.com/1.1/search/tweets.json) requires the search term as a mandatory parameter.

You can find the following code in the twitter_search_tag.py file:

#! /usr/bin/python3

import requests
import requests_oauthlib
import sys
import json

def verify_credentials(auth_obj):
url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
response = requests.get(url, auth=auth_obj)
return response.status_code == 200

def search(auth_obj):
params = {'q': 'python'}
url = 'https://api.twitter.com/1.1/search/tweets.json'
response = requests.get(url, params=params, auth=auth_obj)
return response

if __name__ == '__main__':
auth_obj = init_auth('credentials.txt')
response = search(auth_obj)
print (json.dumps(response.json(),indent = 2))

In this screenshot, we can see the execution of the previous script:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset