Skip to content

Instantly share code, notes, and snippets.

@liannewriting
Last active June 6, 2020 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liannewriting/cd35e68deee092eeab9c288f414d6bc1 to your computer and use it in GitHub Desktop.
Save liannewriting/cd35e68deee092eeab9c288f414d6bc1 to your computer and use it in GitHub Desktop.
youtube-scraping-202002
import pandas as pd
import numpy as np
from datetime import timedelta, datetime
# loading the data
dat = []
with open('sydney.txt') as f:
for line in f:
if 'id="video-title"' in line:
keyword = 'aria-label="'
ind0 = line.find(keyword) + len(keyword)
ind1 = line.find('"', ind0)
vid_info = line[ind0:ind1]
# get the link.
keyword = 'href="'
ind0 = line.find(keyword, ind1) + len(keyword)
ind1 = line.find('"', ind0)
vid_link = line[ind0:ind1]
keyword = ' by Sydney Cummings '
ind1 = vid_info.find(keyword)
vid_title = vid_info[:ind1]
ind0 = ind1 + len(keyword)
ind1 = vid_info.find(' ago ')
vid_ago = vid_info[ind0:ind1]
ind0 = ind1 + len(' ago ')
ind1 = vid_info.find(' ', ind0)
ind1 = vid_info.find(' ', ind1 + 1)
vid_length = vid_info[ind0:ind1]
if vid_length[-1] == ',':
ind1 = vid_info.find(' ', ind1 + 1)
ind1 = vid_info.find(' ', ind1 + 1)
vid_length = vid_info[ind0:ind1]
vid_length_sec = 0
vid_length_arr = vid_length.split(' ')
if len(vid_length_arr) == 2:
if 'minute' in vid_length_arr[1]:
vid_length_sec = int(vid_length_arr[0]) * 60
elif 'hour' in vid_length_arr[1]:
vid_length_sec = int(vid_length_arr[0]) * 60 * 60
else:
vid_length_sec = int(vid_length_arr[0])
elif len(vid_length_arr) >= 4:
if 'minute' in vid_length_arr[1]:
vid_length_sec = int(vid_length_arr[0]) * 60
elif 'hour' in vid_length_arr[1]:
vid_length_sec = int(vid_length_arr[0]) * 60 * 60
else:
vid_length_sec = int(vid_length_arr[0])
if 'minute' in vid_length_arr[3]:
vid_length_sec += int(vid_length_arr[2]) * 60
elif 'hour' in vid_length_arr[3]:
vid_length_sec += int(vid_length_arr[2]) * 60 * 60
else:
vid_length_sec += int(vid_length_arr[2])
if vid_info[ind1-1] == ',':
ind1 += 1
ind1 = vid_info.find(' ', ind1)
ind1 = vid_info.find(' ', ind1+1)
ind0 = ind1 + 1
ind1 = vid_info.find(' ', ind0)
vid_views = int(vid_info[ind0:ind1].replace(',', ''))
row = [vid_title, vid_ago, vid_length_sec, vid_views, vid_link]
dat.append(row)
df_videos = pd.DataFrame(data=dat, columns=['title', 'posted_ago', 'length', 'views', 'link'])
# getting the calories
def get_calories(title):
title_lower = title.lower()
ind1 = title_lower.find('calories')
if ind1 > 0:
ind0 = ind1 - 4
cal = title_lower[ind0:ind1].strip()
try:
cal = int(cal)
except:
cal = np.nan
return cal
return np.nan
df_videos['calories'] = df_videos['title'].map(get_calories)
# assigning the dates, the ones that are irregular are assigned no dates
dtes = ['2020-01-13','2020-01-12','2020-01-11','2020-01-10','2020-01-09','2020-01-08','2020-01-07','2020-01-06','2020-01-05','2020-01-04','2020-01-03','2020-01-02','2020-01-01','2019-12-31','2019-12-30','2019-12-29','2019-12-28','2019-12-27','2019-12-26','2019-12-25','2019-12-24','2019-12-23','2019-12-22','2019-12-21','2019-12-20','2019-12-19','2019-12-18','2019-12-17','2019-12-16','2019-12-15','2019-12-14','2019-12-13','2019-12-12','2019-12-11','2019-12-10','2019-12-09','2019-12-08','2019-12-07','2019-12-06','2019-12-05','2019-12-04','2019-12-03','2019-12-02','2019-12-01','2019-11-30','2019-11-29','2019-11-28','2019-11-27','2019-11-26','2019-11-25','2019-11-24','2019-11-23','2019-11-22','2019-11-21','2019-11-20','2019-11-19','2019-11-18','2019-11-17','2019-11-16','2019-11-15','2019-11-14','2019-11-13','2019-11-12','2019-11-11','2019-11-10','2019-11-09','2019-11-08','2019-11-07','2019-11-06','2019-11-05','2019-11-04','2019-11-03','2019-11-02','2019-11-01','2019-10-31','2019-10-30','2019-10-29','2019-10-28','2019-10-27','2019-10-26','2019-10-25','2019-10-24','2019-10-23','2019-10-22','2019-10-21','2019-10-20','2019-10-19','2019-10-18','2019-10-17','2019-10-16','2019-10-15','2019-10-15','2019-10-14','2019-10-13','2019-10-12','2019-10-11','2019-10-10','2019-10-09','2019-10-08','2019-10-07','2019-10-06','2019-10-05','2019-10-04','2019-10-03','2019-10-02','2019-10-01','2019-09-30','2019-09-30','2019-09-29','2019-09-28','2019-09-27','2019-09-26','2019-09-26','2019-09-25','2019-09-24','2019-09-23','2019-09-22','2019-09-21','2019-09-21','2019-09-20','2019-09-19','2019-09-18','2019-09-17','2019-09-16','2019-09-16','2019-09-15','2019-09-14','2019-09-13','2019-09-12','2019-09-11','2019-09-11','2019-09-10','2019-09-09','2019-09-08','2019-09-07','2019-09-07','2019-09-06','2019-09-05','2019-09-04','2019-09-04','2019-09-03','2019-09-02','2019-09-01','2019-08-31','2019-08-30','2019-08-29','2019-08-28','2019-08-27','2019-08-26','2019-08-25','2019-08-24','2019-08-23','2019-08-22','2019-08-21','2019-08-20','2019-08-19','2019-08-18','2019-08-17','2019-08-16','2019-08-16','2019-08-15','2019-08-14','2019-08-13','2019-08-12','2019-08-11','2019-08-10','2019-08-09','2019-08-08','2019-08-07','2019-08-06','2019-08-05','2019-08-04','2019-08-03','2019-08-02','2019-08-01','2019-07-31','2019-07-30','2019-07-29','2019-07-28','2019-07-27','2019-07-26','2019-07-25','2019-07-24','2019-07-23','2019-07-22','2019-07-21','2019-07-20','2019-07-19','2019-07-18','2019-07-17','2019-07-16','2019-07-15','2019-07-14','2019-07-13','2019-07-12','2019-07-11','2019-07-10','2019-07-09','2019-07-08','2019-07-07','2019-07-06','2019-07-05','2019-07-04','2019-07-03','2019-07-02','2019-07-01','2019-06-30','2019-06-29','2019-06-28','2019-06-27','2019-06-26','2019-06-25','2019-06-24','2019-06-23','2019-06-22','2019-06-21','2019-06-20','2019-06-19','2019-06-18','2019-06-17','2019-06-16','2019-06-15','2019-06-14','2019-06-13','2019-06-12','2019-06-11','2019-06-10','2019-06-09','2019-06-08','2019-06-07','2019-06-06','2019-06-05','2019-06-04','2019-06-03','2019-06-02','2019-06-01','2019-05-31','2019-05-30','2019-05-29','2019-05-28','2019-05-27','2019-05-26','2019-05-25','2019-05-24','2019-05-23','2019-05-22','2019-05-21','2019-05-20','2019-05-19','2019-05-18','2019-05-17','2019-05-16','2019-05-15','2019-05-14','2019-05-13','2019-05-12','2019-05-11','2019-05-10','2019-05-09','2019-05-08','2019-05-07','2019-05-06','2019-05-05','2019-05-04','2019-05-03','2019-05-02','2019-05-01','2019-04-30','2019-04-29','2019-04-28','2019-04-27','2019-04-26','2019-04-25','2019-04-24','2019-04-23','2019-04-22','2019-04-21','2019-04-20','2019-04-19','2019-04-18','2019-04-17','2019-04-16','2019-04-15','2019-04-14','2019-04-13','2019-04-12','2019-04-11','2019-04-10','2019-04-09','2019-04-08','2019-04-07','2019-04-06','2019-04-05','2019-04-04','2019-04-03','2019-04-02','2019-04-01','2019-03-31','2019-03-30','2019-03-29','2019-03-28','2019-03-27','2019-03-26','2019-03-25','2019-03-24','2019-03-23','2019-03-22','2019-03-21','2019-03-20','2019-03-19','2019-03-18','2019-03-17','2019-03-16','2019-03-15','2019-03-14','2019-03-13','2019-03-12','2019-03-11','2019-03-10','2019-03-09','2019-03-08','2019-03-07','2019-03-06','2019-03-05','2019-03-04','2019-03-03','2019-03-02','2019-03-01','2019-02-28','2019-02-27','2019-02-27','2019-02-26','2019-02-25','2019-02-24','2019-02-23','2019-02-22','2019-02-21','2019-02-20','2019-02-19','2019-02-18','2019-02-18','2019-02-18','2019-02-17','2019-02-16','2019-02-15','2019-02-15','2019-02-14','2019-02-13','2019-02-12','2019-02-11','2019-02-10','2019-02-09','2019-02-08','2019-02-07','2019-02-06','2019-02-05','2019-02-04','2019-02-04','2019-02-03','2019-02-03','2019-02-02','2019-02-01','2019-01-31','2019-01-31','2019-01-30','2019-01-29','2019-01-28','2019-01-28','2019-01-27','2019-01-26','2019-01-25','2019-01-25','2019-01-24','2019-01-23','2019-01-22','2019-01-22','2019-01-21','2019-01-21','2019-01-20','2019-01-19','2019-01-18','2019-01-17','2019-01-17','2019-01-16','2019-01-15','2019-01-14','2019-01-13','2019-01-12','2019-01-11','2019-01-11','2019-01-10','2019-01-09','2019-01-08','2019-01-07','2019-01-07','2019-01-06','2019-01-05','2019-01-04','2019-01-04','2019-01-03','2019-01-02','2019-01-01','2018-12-31','2018-12-30','2018-12-29','2018-12-28','2018-12-27','2018-12-26','2018-12-25','2018-12-24','2018-12-23','2018-12-22','2018-12-21','2018-12-20','2018-12-19','2018-12-18','2018-12-17','2018-12-16','2018-12-15','2018-12-14','2018-12-13','2018-12-12','2018-12-11','2018-12-10','2018-12-09','2018-12-08','2018-12-07','2018-12-06','2018-12-05','2018-12-04','2018-12-03','2018-12-02','2018-12-01','2018-11-30','2018-11-29','2018-11-28','2018-11-27','2018-11-26','2018-11-25','2018-11-24','2018-11-23','2018-11-22','2018-11-21','2018-11-20','2018-11-19','2018-11-18','2018-11-17','2018-11-16','2018-11-15','2018-11-14','2018-11-13','2018-11-12','2018-11-11','2018-11-10','2018-11-09','2018-11-08','2018-11-07','2018-11-06','2018-11-05','2018-11-04','2018-11-03','2018-11-02','2018-11-01','2018-10-31','2018-10-30','2018-10-29','2018-10-28','2018-10-27','2018-10-26','2018-10-25','2018-10-24','2018-10-23','2018-10-22','2018-10-21','2018-10-20','2018-10-19','2018-10-18','2018-10-17','2018-10-16','2018-10-15','2018-10-14','2018-10-13','2018-10-12','2018-10-11','2018-10-10','2018-10-09','2018-10-08','2018-10-07','2018-10-06','2018-10-05','2018-10-04','2018-10-03','2018-10-03','2018-10-02','2018-10-01','2018-09-30','2018-09-29','2018-09-28','2018-09-27','2018-09-26','2018-09-25','2018-09-24','2018-09-23','2018-09-22','2018-09-21','2018-09-20','2018-09-19','2018-09-18','2018-09-17','2018-09-16','2018-09-15','2018-09-14','2018-09-13','2018-09-13','2018-09-12','2018-09-11','2018-09-10','2018-09-09','2018-09-08','2018-09-07','2018-09-07','2018-09-06','2018-09-05','2018-09-04','2018-09-03','2018-09-02','2018-09-01','2018-08-31','2018-08-30','2018-08-29','2018-08-28','2018-08-27','2018-08-26','2018-08-25','2018-08-24','2018-08-23','2018-08-22','2018-08-21','2018-08-20','2018-08-19','2018-08-18','2018-08-17','2018-08-16','2018-08-15','2018-08-14','2018-08-13','2018-08-12','2018-08-11','2018-08-10','2018-08-09','2018-08-08','2018-08-07','2018-08-06','2018-08-05','2018-08-04','2018-08-03','2018-08-02','2018-08-01','2018-07-31','2018-07-30','2018-07-29','2018-07-28','2018-07-27','2018-07-26','2018-07-25','2018-07-24','2018-07-23','2018-07-22','2018-07-21','2018-07-20','2018-07-19','2018-07-19','2018-07-18','2018-07-17','2018-07-16','2018-07-15','2018-07-14','2018-07-13','2018-07-12','2018-07-11','2018-07-10','2018-07-09','2018-07-08','2018-07-07','2018-07-06','2018-07-05','2018-07-04','2018-07-03','2018-07-02','2018-07-01','2018-07-01','2018-06-30','2018-06-29','2018-06-28','2018-06-27','2018-06-25','2018-06-24','2018-06-23','2018-06-22','2018-06-21','2018-06-20','2018-06-19','2018-06-18','2018-06-17','2018-06-16','2018-06-15','2018-06-14','2018-06-13','2018-06-12','2018-06-11','2018-06-10','2018-06-09','2018-06-08','2018-06-07','2018-06-06','2018-06-05','2018-06-04','2018-06-03','2018-06-02','2018-06-01','2018-05-31','2018-05-30','2018-05-29','2018-05-28','2018-05-27','2018-05-26','2018-05-25','2018-05-24','2018-05-23','2018-05-23','2018-05-22','2018-05-21','2018-05-20','2018-05-19','2018-05-18','2018-05-17','2018-05-16','2018-05-15','2018-05-15','2018-05-14','2018-05-13','2018-05-11','2018-05-10','2018-05-10','2018-05-09','2018-05-08','2018-05-07','2018-05-06','2018-05-05','2018-05-04','2018-05-03','2018-05-02','2018-04-30','2018-04-29','2018-04-28','2018-04-27','2018-04-26','2018-04-25','2018-04-24','2018-04-23','2018-04-22','2018-04-21','2018-04-20','2018-04-19','2018-04-17','2018-04-16','2018-04-15','2018-04-14','2018-04-13','2018-04-12','2018-04-11','2018-04-10','2018-04-08','2018-04-07','2018-04-06','2018-04-05','2018-04-04','2018-04-02','2018-04-01','2018-03-31','2018-03-30','2018-03-29','2018-03-28','2018-03-27','2018-03-26','2018-03-25','2018-03-24','2018-03-23','2018-03-22','2018-03-21','2018-03-20','2018-03-19','2018-03-18','2018-03-17','2018-03-16','2018-03-15','2018-03-14','2018-03-13','2018-03-12','2018-03-11','2018-03-10','2018-03-09','2018-03-08','2018-03-07','2018-03-06','2018-03-05','2018-03-04','2018-03-03','2018-03-02','2018-03-01','2018-02-28','2018-02-27','2018-02-26','2018-02-25','2018-02-24','2018-02-23','2018-02-22','2018-02-21','2018-02-20','2018-02-20','2018-02-19','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','2018-02-16','2018-02-14','2018-02-13','','','','2018-02-12','2018-02-11','2018-02-08','2018-02-06','2018-02-01','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
df_videos['date'] = pd.to_datetime(dtes)
df_videos['length'] /= 60
df_videos['days_since_posted'] = (pd.to_datetime('2020-01-13') - df_videos['date'])/timedelta(days=1)
@breezeli
Copy link

line 36,remove [ind0:ind1]
line 64,after + is a string,will throw exception
please write some comment

Hey, not sure if you came here after reading the article (https://www.justintodata.com/get-more-youtube-views-with-machine-learning/)
this was written for a one-time project only. so we didn't make it too robust. perhaps only works for the youtube channel we scraped.

sorry ,i knew why they code can‘t run.the YouTube's UI include some Chinese because I'm in China .so many split and string cut can't run.thanks

@liannewriting
Copy link
Author

line 36,remove [ind0:ind1]
line 64,after + is a string,will throw exception
please write some comment

Hey, not sure if you came here after reading the article (https://www.justintodata.com/get-more-youtube-views-with-machine-learning/)
this was written for a one-time project only. so we didn't make it too robust. perhaps only works for the youtube channel we scraped.

sorry ,i knew why they code can‘t run.the YouTube's UI include some Chinese because I'm in China .so many split and string cut can't run.thanks

that's good to know. Glad it works!

@vertex-live
Copy link

vertex-live commented Jun 5, 2020

Just a comment. I had a problem with lines 102 and 103 since I am doing this way after the code was posted and I get an error with the dates since the list is shorter than the array, which will be worse as time goes on.

instead of using line 102, I created a function to parse the date of the video based on the 'posted_ago' context:

def timer(x):
    getDate = ''
    if 'year' in x:
        getDate = pd.to_datetime('today') - pd.DateOffset(years=int(x.split()[0])) 
    elif 'month' in x:
        getDate = pd.to_datetime('today') - pd.DateOffset(months=int(x.split()[0])) 
    elif 'week' in x:
        getDate = pd.to_datetime('today') - pd.DateOffset(weeks=int(x.split()[0])) 
    elif 'day' in x:
        getDate = pd.to_datetime('today') - pd.DateOffset(days=int(x.split()[0]))
    elif 'hour' in x:
        getDate = pd.to_datetime('today')
    return getDate

line 103 would change to this to adjust:

df_videos['date'] = df_videos['posted_ago'].apply(lambda x: timer(x))

and instead of using line 106 I changed the code to adjust for "today's date."

df_videos['days_since_posted'] = (pd.to_datetime('today') - df_videos['date'])/timedelta(days=1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment