Не получается достать список словарей dicts из функции get_content для функции save_parsed_date как аргумент, пытался через global но ничего не получилось. Подскажите, пожалуйста куда копать, вот код:
import requests
from bs4 import BeautifulSoup
import csv
import os
URL = "https://www.youtube.com/user/tkhirianov/videos" # URL = input()
HEADERS = {
"user agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/81.0.4044.129 Safari/537.36",
"accept": "*/*"}
HOST = 'https://www.youtube.com'
FILE = 'dicts.csv'
def get_html(url):
r = requests.get(url, headers=HEADERS)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
dicts = []
# name_chnl = soup.find('title').get_text()
video_box = soup.find_all('h3', attrs={'class': "yt-lockup-title"})
for iterator_find in video_box:
for items in iterator_find.find_all('a'):
dicts.append(
{
'title': items.attrs['title'],
'href': HOST + items.attrs['href'],
}
)
# print(dicts)
def save_parsed_date(dicts, path):
with open(path, 'w', newline='') as file:
w = csv.writer(file, delimiter=';')
w.writerow(['Название видео', 'Ссылка на видео'])
for i in dicts:
w.writerow((i['title'], i['href']))
def parse():
html = get_html(URL)
dicts = []
if html.status_code == 200:
get_content(html.text)
save_parsed_date(dicts, FILE)
os.startfile(FILE)
else:
print('\033[31m ERROR status_code = {}'.format(html.status_code))
parse()