Search code examples
pythonlistweb-scrapingscreen-scrapingtweepy

How to get retweet count for a certain tweet using tweepy or any other python method?


Is it possible to get retweet count for a certain tweet using tweepy python library? I want to get retweet count for every tweet which published from a particular twitter account. Is there are any python method with using tweepy for that? I have tried using beautifulsoup. But I am in the trouble with getting count for every tweet. It returns value only for the defined tweet ID. So how can I change this code to get values for each and every published tweet from that twitter account?

        id=[[tweet.id]for tweet in alltweets]


        html = requests.get("https://twitter.com/%s/status/%s" % ("usename", "userID"))
        soup = BeautifulSoup(html.text, 'lxml')

        comments = soup.find_all('span', attrs={'class': 'ProfileTweet-actionCountForAria'})[0].contents

    outtweets = [{'ID': tweet.id_str, 'Text': tweet.text, 'Date': tweet.created_at, 'author': tweet.user.screen_name,
                  'retweet-count': tweet.retweet_count, 'favourites-count': tweet.favorite_count, 'language': tweet.lang,
                  'reply-count': comments}for tweet in alltweets]

Solution

  • The below code can use for this problem.

    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    
    for page in range(0, 50):
        url = "https://twitter.com/(user.screen.name)".format(page)
        html = urlopen(url)
        soup = BeautifulSoup(html, "html.parser")
        tweets = soup.find_all("li", attrs={"class": "js-stream-item"})
    
    for tweet in tweets:
        try:
            if tweet.find('p',{"class":'tweet-text'}):
                tweet_text = tweet.find('p', {"class": 'tweet-text'}).text.encode('utf8').strip()
                retweets = tweet.find('span', {"class": "ProfileTweet-action--retweet"}).text.strip()
                print(tweet_user, "|", retweets)
        except: AttributeError