Skip to content

Instantly share code, notes, and snippets.

@vivekiyer114
Created November 28, 2016 03:11
Show Gist options
  • Save vivekiyer114/cc6f118779088aef603770fc804ae50c to your computer and use it in GitHub Desktop.
Save vivekiyer114/cc6f118779088aef603770fc804ae50c to your computer and use it in GitHub Desktop.
A simple youtube views extracter (My first python web scraper :) )
from bs4 import BeautifulSoup
import urllib2
s=raw_input('enter query:')
s=s.replace(" ","+")
print(s)
s1='https://www.youtube.com/results?search_query='+s
html=urllib2.urlopen(s1)
resp=html.read()
soup=BeautifulSoup(resp,'html.parser')
c=0
titles=[1,2,3,4,5] ;
for b in soup.find_all('a'):
if(b.has_attr('dir')):
if(b['dir']=='ltr'):
if(c<5):
print(str(c+1)+")"+(b.text))
titles[c]=b['href']
c=c+1
r=input("\nWhich one?")
s2='https://www.youtube.com'+titles[r-1]
html1=urllib2.urlopen(s2)
resp1=html1.read()
soup1=BeautifulSoup(resp1,'html.parser')
for a in soup1.find_all('meta'):
if(a.has_attr('itemprop')):
if(a['itemprop']=='interactionCount'):
print("No of views ="+str(a['content']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment