Created
January 30, 2019 19:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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