pythonweb-scrapingtwitterscrapytweepy

retrieve all tweets and retweets user ids for hashtag


how can I scraping tweets with meta data(tweet text - userid - retweets user id) based on hashtag? I'm trying to use tweepy api code :

import tweepy
import csv
import pandas as pd
####input your credentials here
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
#USA
# Open/Create a file to append data
csvFile = open('ua.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

for tweet in tweepy.Cursor(api.search,q="#usa",count=100,
                           lang="en",
                           since="2018-04-03").items():
    print (tweet.created_at, tweet.text)
    csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])


Solution

  • You haven't explained what error you are seeing, however:

    The legacy Twitter v1.1 standard search API (which is what api.search in Tweepy is calling) can only provide you data from the past 7 days. If you need Tweets since April 2018, you will need to use the premium full-archive search API.

    tweets with meta data(tweet text - userid - retweets user id)

    You would need to modify your code to include the value of tweet.user.id. There's no Retweet count available in the legacy v1.1 Twitter API, but this value is part of the new data format in API v2.