· 5 years ago · Feb 07, 2021, 11:30 AM
1"""
2Sunday, February 7th of 2021 at 06:24.
3Made in Python [3.9.1]
4Scrape "youtube.com" without API key.
5 - Returns video ID, video title, video thumbnail,
6 video length, video views, video owner, owner badge,
7 published at, likes, dislikes and description.
8 - Includes a few ways to display the values but you
9 can modify and change it how ever you want anyway.
10 - Probably some other additional information that I
11 did not bother to add because I deemed them unnecessary,
12 though these may still be viewed if you print it out yourself.
13 - My IDLE crashed several times in the making of this.
14Dependencies:
15 requests (pip install requests)
16 - If you want, replace with urllib.request I guess?
17By [Vissle Drissle]
18Version 2.0
19"""
20
21import re
22import json
23import random
24import requests
25
26def youtube(data):
27 try:
28 """ [VIDEO EXAMPLE]
29 Video id:
30 got["videoId"] = HQnC1UHBvWA
31 Video title:
32 got["title"]["runs"][0]["text"] = Porter Robinson & Madeon - Shelter (Official Audio)
33 got["title"]["accessibility"]["accessibilityData"]["label"] = Porter Robinson & Madeon - Shelter (Official Audio) by Madeon 4 years ago 3 minutes, 38 seconds 48,107,185 views
34 Video thumbnail:
35 got["thumbnail"]["thumbnails"][0]["url"][:44] = https://i.ytimg.com/vi/HQnC1UHBvWA/hq720.jpg
36 Video length:
37 got["lengthText"]["simpleText"] = 3:38
38 got["lengthText"]["accessibility"]["accessibilityData"]["label"] = 3 minutes, 38 seconds
39 Video views:
40 got["viewCountText"]["simpleText"] = 48,107,185 views
41 got["shortViewCountText"]["simpleText"] = 48M views
42 Video owner:
43 got["ownerText"]["runs"][0]["text"] = Madeon
44 got["ownerText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"] = UCqMDNf3Pn5L7pcNkuSEeO3w
45 got["ownerText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["canonicalBaseUrl"] = /channel/UCqMDNf3Pn5L7pcNkuSEeO3w
46 Video owner badge:
47 got["ownerBadges"][0]["metadataBadgeRenderer"]["tooltip"] = Official Artist Channel OR Verified
48 Video published:
49 got["publishedTimeText"]["simpleText"] = 4 years ago OR Premiered
50 extra["dateText"]["simpleText"] = Aug 11, 2016
51 Video likes:
52 extra["videoActions"]["menuRenderer"]["topLevelButtons"][0]["toggleButtonRenderer"]["defaultText"]["accessibility"]["accessibilityData"]["label"] = 471,014 likes
53 extra["videoActions"]["menuRenderer"]["topLevelButtons"][0]["toggleButtonRenderer"]["defaultText"]["simpleText"] = 471K
54 Video dislikes:
55 extra["videoActions"]["menuRenderer"]["topLevelButtons"][1]["toggleButtonRenderer"]["defaultText"]["accessibility"]["accessibilityData"]["label"] = 6,690 dislikes
56 extra["videoActions"]["menuRenderer"]["topLevelButtons"][1]["toggleButtonRenderer"]["defaultText"]["simpleText"] = 6.6K
57 Video likes and dislikes:
58 extra["sentimentBar"]["sentimentBarRenderer"]["tooltip"] = 471,014 / 6,690
59 Video short description:
60 "".join([x["text"] for x in got["descriptionSnippet"]["runs"]])
61 Video full description:
62 re.search('"shortDescription":"(.*?)","isCrawlable"', extra).group(1).replace("\\n", "\n")
63 """
64 get = requests.get(f"https://www.youtube.com/results?search_query={data}&sp=EgIQAQ%253D%253D").text
65 result = re.findall('{"videoRenderer":(.*?)("Now playing"}]}}}]}|true}}})', get)
66 choice = random.choice(result)
67 got = json.loads(choice[0] + choice[1])
68 link = f'https://youtu.be/{got["videoId"]}'
69 step = requests.get(link).text
70 additional = re.search('{"videoPrimaryInfoRenderer":(.*?)},{"videoSecondaryInfoRenderer"', step).group(1)
71 extra = json.loads(additional)
72 title, length, views = got["title"]["runs"][0]["text"], got["lengthText"]["simpleText"], got["viewCountText"]["simpleText"]
73 owner = (f'({got["ownerBadges"][0]["metadataBadgeRenderer"]["tooltip"]}) ' if "ownerBadges" in got else "") + got["ownerText"]["runs"][0]["text"]
74 published = f'({got["publishedTimeText"]["simpleText"]} on {extra["dateText"]["simpleText"]})'
75 ratio = f'[{extra["sentimentBar"]["sentimentBarRenderer"]["tooltip"]}]'
76 print(f'{title}\n{length} - {views}\n{ratio}\nUploaded by {owner} {published}\n{link}')
77 except Exception as error: print(error)