w3resource
Python Web Scraping Exercises

Python Web Scraping: Get the number of post on Twitter liked by a given account

Python Web Scraping: Exercise-18 with Solution

Write a Python program to get the number of post on Twitter liked by a given 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:
    favorite_box = bs.find('li',{'class':'ProfileNav-item ProfileNav-item--favorites'})
    favorite = favorite_box.find('a').find('span',{'class':'ProfileNav-value'})
    print("Number of post {}  liked are {}: ".format(handle,favorite.get('data-count')))

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

Output

Input your account name on Twitter:  @prasanta
Number of post @prasanta  liked are 119: 

Flowchart:

Python Web Scraping Flowchart: Get the number of post on Twitter liked by a given 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 count number of tweets by a given Twitter account.

What is the difficulty level of this exercise?



Python: Tips of the Day

Python: Debug With the Print() Function

>>> for i in range(5):
...     print(i, end=', ' if i < 4 else '\n')
... 
0, 1, 2, 3, 4
>>> for i in range(5):
...     print(f'{i} & {i*i}', end=', ' if i < 4 else '\n')
... 
0 & 0, 1 & 1, 2 & 4, 3 & 9, 4 & 16