Skip to content

Instantly share code, notes, and snippets.

@Vontux
Created January 30, 2019 19:33
Show Gist options
  • Save Vontux/760d35566566ff7919d3d813825413f1 to your computer and use it in GitHub Desktop.
Save Vontux/760d35566566ff7919d3d813825413f1 to your computer and use it in GitHub Desktop.
youtube video view count script that takes a youtube url as a commandline argument and prints its view count to the console, uses code from https://stackoverflow.com/questions/42926027/how-can-i-get-some-data-from-a-video-youtube-python
import sys
import urllib2
source = sys.argv.pop()
response = urllib2.urlopen(source)
html = response.read()
wordBreak = ['<','>']
html = list(html)
i = 0
while i < len(html):
if html[i] in wordBreak:
html[i] = ' '
i += 1
#The block above is just to make the html.split() easier.
html = ''.join(html)
html = html.split()
dataSwitch = False
numOfViews = ''
for element in html:
if element == '/div':
dataSwitch = False
if dataSwitch:
numOfViews += str(element)
if element == 'class="watch-view-count"':
dataSwitch = True
print (numOfViews)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment