I'm having trouble searching links on youtube, I tried google-api-python-client, along with the sample codes converted to python3, I tried to use the code below, the youtube search module hosted on pypi, I checked the key API, and it wasn't her. The code returns a bs4 error that I tried to solve, when I solve it it returns an empty list, with no link.
Can anyone tell me a way to do a search on youtube and get the links using Python? Thank you very much for your help, I am using Python 3.8.3 and Windows 10 x86.
youtube-search module: https://pypi.org/project/youtube-search/
samples code: https://developers.google.com/youtube/v3/code_samples/python?hl=pt-br (Python 2.x, I'm tryed a conversion to python3.x)
My code with bs4 and urllib:
import urllib.request
from bs4 import BeautifulSoup
textToSearch = 'hello world'
query = urllib.parse.quote(textToSearch)
url = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
print(soup.findAll(attrs={"class": "yt-uix-tile-link"}))
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
print('https://www.youtube.com' + vid['href'])
There is a similar module for your requirement (supports both async & sync):
https://github.com/alexmercerind/youtube-search-python
You can use it in following way:
from youtubesearchpython import VideosSearch
videosSearch = VideosSearch('NoCopyrightSounds', limit = 2)
print(videosSearch.result())
{
"result": [
{
"type": "video",
"id": "K4DyBUG242c",
"title": "Cartoon - On & On (feat. Daniel Levi) [NCS Release]",
"publishedTime": "5 years ago",
"duration": "3:28",
"viewCount": {
"text": "389,673,774 views",
"short": "389M views"
},
"thumbnails": [
{
"url": "https://i.ytimg.com/vi/K4DyBUG242c/hqdefault.jpg?sqp=-oaymwEjCOADEI4CSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLBkTusCwcZQlmVAaRQ5rH-mvBuA1g",
"width": 480,
"height": 270
}
],
"descriptionSnippet": [
{
"text": "NCS: Music Without Limitations NCS Spotify: http://spoti.fi/NCS Free Download / Stream: http://ncs.io/onandon \u25bd Connect with\u00a0..."
}
],
"channel": {
"name": "NoCopyrightSounds",
"id": "UC_aEa8K-EOJ3D6gOs7HcyNg",
"thumbnails": [
{
"url": "https://yt3.ggpht.com/a-/AOh14GhS0G5FwV8rMhVCUWSDp36vWEvnNs5Vl97Zww=s68-c-k-c0x00ffffff-no-rj-mo",
"width": 68,
"height": 68
}
],
"link": "https://www.youtube.com/channel/UC_aEa8K-EOJ3D6gOs7HcyNg"
},
"accessibility": {
"title": "Cartoon - On & On (feat. Daniel Levi) [NCS Release] by NoCopyrightSounds 5 years ago 3 minutes, 28 seconds 389,673,774 views",
"duration": "3 minutes, 28 seconds"
},
"link": "https://www.youtube.com/watch?v=K4DyBUG242c",
"shelfTitle": null
},
{
"type": "video",
"id": "yJg-Y5byMMw",
"title": "Warriyo - Mortals (feat. Laura Brehm) [NCS Release]",
"publishedTime": "3 years ago",
"duration": "3:50",
"viewCount": {
"text": "153,353,801 views",
"short": "153M views"
},
"thumbnails": [
{
"url": "https://i.ytimg.com/vi/yJg-Y5byMMw/hqdefault.jpg?sqp=-oaymwEjCOADEI4CSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDY-mve79IweErMo-71AsKEIB1m0A",
"width": 480,
"height": 270
}
],
"descriptionSnippet": [
{
"text": "NCS: Music Without Limitations NCS Spotify: http://spoti.fi/NCS Free Download / Stream: http://ncs.io/mortals Connect with NCS:\u00a0..."
}
],
"channel": {
"name": "NoCopyrightSounds",
"id": "UC_aEa8K-EOJ3D6gOs7HcyNg",
"thumbnails": [
{
"url": "https://yt3.ggpht.com/a-/AOh14GhS0G5FwV8rMhVCUWSDp36vWEvnNs5Vl97Zww=s68-c-k-c0x00ffffff-no-rj-mo",
"width": 68,
"height": 68
}
],
"link": "https://www.youtube.com/channel/UC_aEa8K-EOJ3D6gOs7HcyNg"
},
"accessibility": {
"title": "Warriyo - Mortals (feat. Laura Brehm) [NCS Release] by NoCopyrightSounds 3 years ago 3 minutes, 50 seconds 153,353,801 views",
"duration": "3 minutes, 50 seconds"
},
"link": "https://www.youtube.com/watch?v=yJg-Y5byMMw",
"shelfTitle": null
}
]
}
The search result from this library is very detailed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With