Created
December 11, 2019 13:26
-
-
Save viniroger/92cc39ad9793eda1441183924994b6ef to your computer and use it in GitHub Desktop.
Get title and description from youtube videos
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
#!/usr/bin/env python3.7.5 | |
# -*- Coding: UTF-8 -*- | |
# Read a file with youtube URLs, get info and save into CSV file | |
import requests | |
from bs4 import BeautifulSoup as bs | |
import pandas as pd | |
def get_video_info(url): | |
""" | |
Get information from YouTube video | |
""" | |
# download HTML code | |
content = requests.get(url) | |
# create beautiful soup object to parse HTML | |
soup = bs(content.content, "html.parser") | |
# initialize the result | |
result = {} | |
# video title | |
result['title'] = soup.find("span", attrs={"class": "watch-title"}).text.strip() | |
# video description | |
result['description'] = soup.find("p", attrs={"id": "eow-description"}).text | |
return result | |
# Read files with links | |
filename = 'urls_youtube.txt' | |
lines = [line.rstrip('\n') for line in open(filename)] | |
flag = 0 | |
df = pd.DataFrame(columns=['title', 'description']) | |
for url in lines: | |
print(url) | |
dictionary = get_video_info(url) | |
df = pd.DataFrame(columns=['title', 'description']) | |
df = df.append(dictionary, ignore_index=True) | |
# Write to CSV | |
if flag == 0: | |
df.to_csv('title_desc.csv', index=False) | |
flag = 1 | |
else: | |
df.to_csv('title_desc.csv', index=False, mode='a', header=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment