A simple search using only requests and Beautiful soup4.
Y = Youtube([query], result=[num])
query is a search word. Japanese support. num is the number of search results. Up to 20 at the moment due to pagination. Please play around here by yourself.
Y.url >>Search result url list
Y.title >>Title list
Y.id >>ID list (ID? Unique number like JaDJbrwi0gk)
Y.embed >>URL list for pasting
Y.time >>Playback time list (may not be in demand)
Example)
print(Youtube("Un-Jash",result=5).url)
>> ['https://www.youtube.com/watch?v=JaDJbrwi0gk', 'https://www.youtube.com/watch?v=yGPAyqxrado', 'https://www.youtube.com/watch?v=tGl2Dp1v0gw', 'https://www.youtube.com/watch?v=myp35PyYk1A', 'https://www.youtube.com/watch?v=Tt1rkp3h1pM', 'https://www.youtube.com/watch?v=XIJqsqOCorw']
Y.select() >>Select one from the search results and return a dictionary for the above information.
key = "url","title","id","embed","time"
Example)
movie = Y.select()
print(movie["url"]) >> https://www.youtube.com/watch?v=JaDJbrwi0gk
It may be efficient to combine it with other download APIs.
import requests
from bs4 import BeautifulSoup
class Youtube():
def __init__(self,query,result=10): #up to max20
search_url = "https://www.youtube.com/results?search_query=" + query
req = requests.get(search_url)
soup = BeautifulSoup(req.text.encode(req.encoding).decode('utf-8','strict'),"html5lib")
h3s = soup.find_all("h3", {"class":"yt-lockup-title"})[0:result+1]
self.data = [h3 for h3 in h3s]
self.url = ["https://www.youtube.com" + h3.a.get('href') for h3 in h3s]
self.title = [h3.a.get("title") for h3 in h3s]
self.id = [h3.a.get("href").split("=")[-1] for h3 in h3s]
self.embed = ["https://www.youtube.com/embed/" + h3.a.get("href").split("=")[-1] for h3 in h3s]
self.time = [h3.span.text.replace(" -length: ","").replace("。","") for h3 in h3s]
self.info = [h3.text for h3 in h3s] # >>title-Length: 00:00。
def select(self):
values = {"url":self.url,"title":self.title,"id":self.id,"embed":self.embed,"time":self.time}
info = self.info
for i in range(len(info)):
print("%s:%s" % (i,info[i]))
while True:
try:
num = int(input("number:"))
break
except:
print("Please enter the number correctly.")
results = {
"url":values["url"][num],
"title":values["title"][num],
"id":values["id"][num],
"embed":values["embed"][num],
"time":values["time"][num],
}
return results
if __name__ == '__main__':
Y = Youtube(input("Search word:"),result=5)
movie = Y.select()
print(movie["url"])
Recommended Posts