Web Scraping in Python: Sample Project with Source Code.

Hi There.

Today, I am sharing with you a new Sample Python Project that will help beginners boost there programming skills ad understand more concepts.

Python Youtube Sample Scraper

Its a simple Web scraper python script that allows you to read the number of views of each video in the first page of  Youtube page search with a specific keyword.

Here is the source code:

from bs4 import BeautifulSoup #pip install BeautifulSoup4
import requests #pip install requests

#Enter a Keyword
Keyword = input("Enter a Youtube Search Keyword: ")

#Get The Page
res = requests.get('https://www.youtube.com/results?search_query=' + Keyword)

#Get the Body With lxml parser
bs = BeautifulSoup(res.text, 'lxml') #pip install lxml

#Get all elements with class yt-lockup-meta-info (the views Tag class)
ViewsElements = bs.find_all('ul',class_='yt-lockup-meta-info')

totalview = 0
for obj in ViewsElements:
    lis= obj.findChildren()
    for li in lis:
        if li.string.endswith('views'):
            VideoViews = li.text.replace(' views','').replace(',','')
            totalview = totalview + int(VideoViews)
print('Total Views: ' + str(totalview))

Sharing is caring!

2 thoughts on “Web Scraping in Python: Sample Project with Source Code.”

  1. i got error while doing this i have install all requires libraries but it only show total views = 0 so kindly help.
    Expect us

Leave a Comment

Your email address will not be published. Required fields are marked *