{"id":95,"date":"2023-03-10T07:35:40","date_gmt":"2023-03-10T07:35:40","guid":{"rendered":"https:\/\/smartsource.com.sg\/blog\/?p=95"},"modified":"2023-03-10T07:35:40","modified_gmt":"2023-03-10T07:35:40","slug":"get-tweets-and-replies-from-twitter-using-python","status":"publish","type":"post","link":"https:\/\/smartsource.com.sg\/blog\/index.php\/2023\/03\/10\/get-tweets-and-replies-from-twitter-using-python\/","title":{"rendered":"Get Tweets and replies from twitter using Python"},"content":{"rendered":"\n<p>Here&#8217;s some sample Python code that uses the Twitter API to collect tweets and replies and saves them in a dataset:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tweepy\r\nimport pandas as pd\r\n\r\n# set up the Twitter API credentials\r\nconsumer_key = 'your_consumer_key'\r\nconsumer_secret = 'your_consumer_secret'\r\naccess_token = 'your_access_token'\r\naccess_secret = 'your_access_secret'\r\n\r\n# authenticate with the Twitter API\r\nauth = tweepy.OAuthHandler(consumer_key, consumer_secret)\r\nauth.set_access_token(access_token, access_secret)\r\napi = tweepy.API(auth)\r\n\r\n# search for tweets containing a particular keyword or hashtag\r\nquery = 'python'\r\ntweets = tweepy.Cursor(api.search_tweets, q=query, tweet_mode='extended').items(1000)\r\n\r\n# create a list to store the tweet and reply data\r\ndata = &#91;]\r\n\r\n# loop through each tweet and get its replies\r\nfor tweet in tweets:\r\n    tweet_data = {\r\n        'text': tweet.full_text,\r\n        'user': tweet.user.screen_name,\r\n        'created_at': tweet.created_at\r\n    }\r\n    replies = tweepy.Cursor(api.search_tweets, q='to:'+tweet.user.screen_name, since_id=tweet.id, tweet_mode='extended').items()\r\n    for reply in replies:\r\n        reply_data = {\r\n            'text': reply.full_text,\r\n            'user': reply.user.screen_name,\r\n            'created_at': reply.created_at,\r\n            'in_reply_to': tweet.id\r\n        }\r\n        data.append(reply_data)\r\n    data.append(tweet_data)\r\n\r\n# convert the list of tweet and reply data into a Pandas DataFrame\r\ndf = pd.DataFrame(data)\r\n\r\n# save the DataFrame as a CSV file\r\ndf.to_csv('twitter_data.csv', index=False)\r<\/code><\/pre>\n\n\n\n<p>In this code, we first set up the Twitter API credentials using the values for <code>consumer_key<\/code>, <code>consumer_secret<\/code>, <code>access_token<\/code>, and <code>access_secret<\/code>. We then use Tweepy to authenticate with the Twitter API and search for tweets containing a particular keyword or hashtag (<code>query<\/code>).<\/p>\n\n\n\n<p>We loop through each tweet that we find and use Tweepy to get its replies by searching for tweets that are directed to the same user and have a higher ID than the current tweet. We store the tweet and reply data in a list called <code>data<\/code>, which we then convert into a Pandas DataFrame and save as a CSV file.<\/p>\n\n\n\n<p>The resulting CSV file will contain columns for the text of the tweet and reply, the user who posted the tweet and reply, the timestamp for when the tweet and reply were created, and the ID of the tweet that the reply is in response to.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s some sample Python code that uses the Twitter API to collect tweets and replies and saves them in a dataset: In this code, we first set up the Twitter&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[79,64,80,78],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-api","tag-python","tag-scraping","tag-twitter"],"_links":{"self":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":1,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/95\/revisions\/96"}],"wp:attachment":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}