Skip to content

Commit 0850c76

Browse files
authored
Fixing ValueError thrown by int('')
YT can return 'No likes' / 'No dislikes'. The digits from this string give ''. Casting '' to an int gives ValueError.
1 parent 272e14b commit 0850c76

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

web-scraping/youtube-extractor/extract_video_info.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ def get_video_info(url):
2929
result["tags"] = ', '.join([ meta.attrs.get("content") for meta in soup.find_all("meta", {"property": "og:video:tag"}) ])
3030
# number of likes
3131
text_yt_formatted_strings = soup.find_all("yt-formatted-string", {"id": "text", "class": "ytd-toggle-button-renderer"})
32-
result["likes"] = int(''.join([ c for c in text_yt_formatted_strings[0].attrs.get("aria-label") if c.isdigit() ]))
32+
result["likes"] = ''.join([ c for c in text_yt_formatted_strings[0].attrs.get("aria-label") if c.isdigit() ])
33+
result["likes"] = 0 if result['likes'] == '' else int(result['likes'])
3334
# number of dislikes
34-
result["dislikes"] = int(''.join([ c for c in text_yt_formatted_strings[1].attrs.get("aria-label") if c.isdigit() ]))
35+
result["dislikes"] = ''.join([ c for c in text_yt_formatted_strings[1].attrs.get("aria-label") if c.isdigit() ])
36+
result['dislikes'] = 0 if result['dislikes'] == '' else int(result['dislikes'])
3537

3638
# channel details
3739
channel_tag = soup.find("yt-formatted-string", {"class": "ytd-channel-name"}).find("a")
@@ -66,4 +68,4 @@ def get_video_info(url):
6668
print(f"\nDescription: {data['description']}\n")
6769
print(f"\nChannel Name: {data['channel']['name']}")
6870
print(f"Channel URL: {data['channel']['url']}")
69-
print(f"Channel Subscribers: {data['channel']['subscribers']}")
71+
print(f"Channel Subscribers: {data['channel']['subscribers']}")

0 commit comments

Comments
 (0)