Search code examples
pythonpython-3.xtwittertweepy

Is there any way to get the number of comments on a tweet using python?


I am using Tweepy and it does not seem like there is a way to scrape the number of comments on a particular tweet from a user. I can use the tweet.favorite_count and tweet.retweet_count to get favorites and retweets but I am looking for a way to get the number of comments on that post. I don't even need to see what the comments are. Just the quantity. Thanks!


Solution

  • I believe you meant replies. Anyway, all you have to do is carefully inspect the page source (CTRL+F and search for "replies"), so you can know what to look for in a BeautifulSoup object later on:

    import requests
    from bs4 import BeautifulSoup
    
    html = requests.get('https://twitter.com/Cristiano/status/912028229011169281')
    soup = BeautifulSoup(html.text, 'lxml')
    
    comments = soup.find_all('span', attrs={'class':'ProfileTweet-actionCountForAria'})[0].contents
    
    print(*comments)
    

    ...output:

    9,370 replies