Python Web Scraping: Get the number of followers of a given twitter account
Python Web Scraping: Exercise-16 with Solution
Write a Python program to get the number of followers of 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:
follow_box = bs.find('li',{'class':'ProfileNav-item ProfileNav-item--followers'})
followers = follow_box.find('a').find('span',{'class':'ProfileNav-value'})
print("Number of followers: {} ".format(followers.get('data-count')))
except:
print('Account name not found...')
Sample Output:
Input your account name on Twitter: @prasanta Number of followers: 61
Flowchart:
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 Pinterest accounts maintained by U.S. State Department embassies and missions.
Next: Write a Python program to get the number of following on Twitter.
What is the difficulty level of this exercise?
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://www.w3resource.com/python-exercises/web-scraping/web-scraping-exercise-16.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics