w3resource

Python Web Scraping: Count number of tweets by a given Twitter account

Python Web Scraping: Exercise-19 with Solution

Write a Python program to count number of tweets by a given Twitter account.

Sample Solution:

Python Code:

from bs4 import BeautifulSoup
import requests

handle = input('Input your account name on Twitter: ')
temp = requests.get('https://twitter.com/'+handle)
bs = BeautifulSoup(temp.text,'lxml')

try:
    tweet_box = bs.find('li',{'class':'ProfileNav-item ProfileNav-item--tweets is-active'})
    tweets= tweet_box.find('a').find('span',{'class':'ProfileNav-value'})
    print("{} tweets {} number of tweets.".format(handle,tweets.get('data-count')))

except:
    print('Account name not found...')
  
 

Sample Output:

Input your account name on Twitter:  @prasanta
@prasanta tweets 36 number of tweets.

Flowchart:

Python Web Scraping Flowchart: Count number of tweets by a given Twitter account.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get the number of following on Twitter.
Next: Write a Python program to scrap number of tweets of a given Twitter account.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/web-scraping/web-scraping-exercise-19.php