Я слежу за этим репозиторием https://github.com/DataSnaek/Trending-YouTube-Scraper, чтобы очистить популярные видео на YouTube.
Я правильно настроил код страны и ключ API. Тем не менее, я хочу добавить продолжительность видео в мой файл данных. Я искал YouTube API и пробовал эту кодировку (добавьте несколько кодов, связанных с contentDetails и длительностью):
...
# Used to identify columns, currently hardcoded order
header = ["video_id"] + snippet_features + ["trending_date", "tags", "length", "view_count", "likes", "dislikes",
"comment_count", "thumbnail_link", "comments_disabled",
"ratings_disabled", "description"]
...
def api_request(page_token, country_code):
# Builds the URL and requests the JSON from it
request_url = f"https://www.googleapis.com/youtube/v3/videos?part=id,statistics,contentDetails,snippet{page_token}chart=mostPopular&hl=vi®ionCode={country_code}&maxResults=50&key={api_key}"
request = requests.get(request_url)
if request.status_code == 429:
print("Temp-Banned due to excess requests, please wait and continue later")
sys.exit()
return request.json()
...
# Snippet, statistics and contentDetails are sub-dicts of video, containing the most useful info
snippet = video['snippet']
statistics = video['statistics']
contentdetails = video['contentDetails']
...
# The following are special case features which require unique processing, or are not within the snippet dict
description = snippet.get("description", "")
thumbnail_link = snippet.get("thumbnails", dict()).get("default", dict()).get("url", "")
length = contentdetails.get("duration", "")
trending_date = time.strftime("%y.%d.%m")
tags = get_tags(snippet.get("tags", ["[none]"]))
view_count = statistics.get("viewCount", 0)
...
if 'duration' in contentdetails:
length = contentdetails['duration']
else:
length = "0"
# Compiles all of the various bits of info into one consistently formatted line
line = [video_id] + features + [prepare_feature(x) for x in
[trending_date, tags, length, view_count, likes, dislikes,
comment_count, thumbnail_link, comments_disabled,
ratings_disabled, description]]
lines.append(",".join(line))
return lines
...
Но фактический вывод - это просто заголовки:
video_id, название, publishedAt, channelId, channelTitle, CategoryId, trending_date, теги, длина, view_count, любит, не любит, COMMENT_COUNT, thumbnail_link, comments_disabled, ratings_disabled, описание
Большое спасибо!