Python youtube search how to get "link"?
Question:
ı am trying to write a program to search anything on youtube after ı want to open the first page.
with webbrowser module.
from youtube_search import YoutubeSearch
import webbrowser
results = YoutubeSearch('eminem', max_results=10).to_json()
print(results)
and this lines gives me;
{"videos": [{"title": "Eminem - Venom", "link": "/watch?v=8CdcCD5V-d8", "id": "8CdcCD5V-d8"}, {"title": "Eminem - Lose Yourself [HD]", "link": "/watch?v=_Yhyp-_hX2s", "id": "_Yhyp-_hX2s"}, {"title": "Eminem - Without Me (Official Video)", "link": "/watch?v=YVkUvmDQ3HY", "id": "YVkUvmDQ3HY"}, {"title": "Eminem - Rap God (Explicit) [Official Video]", "link": "/watch?v=XbGs_qK2PQA", "id": "XbGs_qK2PQA"}, {"title": "Eminem - Not Afraid (Official Video)", "link": "/watch?v=j5-yKhDd64s", "id": "j5-yKhDd64s"}, {"title": "Eminem - Mockingbird (Official Music Video)", "link": "/watch?v=S9bCLPwzSC0", "id": "S9bCLPwzSC0"}, {"title": "Eminem - When I'm Gone (Official Music Video)", "link": "/watch?v=1wYNFfgrXTI", "id": "1wYNFfgrXTI"}, {"title": "Eminem - Cleanin' Out My Closet (Official Video)", "link": "/watch?v=RQ9_TKayu9s", "id": "RQ9_TKayu9s"}, {"title": "Eminem - Till I Collapse [HD]", "link": "/watch?v=ytQ5CYE1VZw", "id": "ytQ5CYE1VZw"}, {"title": "Eminem - Love The Way You Lie ft. Rihanna", "link": "/watch?v=uelHwf8o7_U", "id": "uelHwf8o7_U"}]}
now, how can ı reach to “link” to open the song?
ı tried this but gives me error.
a = results.get("link")
webbrowser.open("www.youtube.com/" + "a")
which is;
AttributeError: 'str' object has no attribute 'get'
how can ı fix this problem? why results variable is str object why is not dictionary? where is my mistake?
and also ı am working on Ubuntu 19.10
Answers:
The answer is because is a string not a dictionary. You need to convert it to a dictionary to use the get method.
Here is some simple code to get started:
from youtube_search import YoutubeSearch
import json
results = YoutubeSearch('eminem', max_results=10).to_json()
results_dict = json.loads(results)
Now, you are going to return 10 results and you want the links for each song (or channel).
for v in results_dict['videos']:
print('https://www.youtube.com' + v['link'])
If you want to see more, use the type
function:
type(results)
and type(results_dict)
results in different output.
You are converting the results to a JSON string. Don’t do that. Instead, call to_dict()
to get the results as a Python dictionary:
results = YoutubeSearch('eminem', max_results=10).to_dict()
for v in results:
print('https://www.youtube.com' + v['link'])
Note:
In a more recent version as of August 2022, the 'link']
key is changed to 'url_suffix'
.
ı am trying to write a program to search anything on youtube after ı want to open the first page.
with webbrowser module.
from youtube_search import YoutubeSearch
import webbrowser
results = YoutubeSearch('eminem', max_results=10).to_json()
print(results)
and this lines gives me;
{"videos": [{"title": "Eminem - Venom", "link": "/watch?v=8CdcCD5V-d8", "id": "8CdcCD5V-d8"}, {"title": "Eminem - Lose Yourself [HD]", "link": "/watch?v=_Yhyp-_hX2s", "id": "_Yhyp-_hX2s"}, {"title": "Eminem - Without Me (Official Video)", "link": "/watch?v=YVkUvmDQ3HY", "id": "YVkUvmDQ3HY"}, {"title": "Eminem - Rap God (Explicit) [Official Video]", "link": "/watch?v=XbGs_qK2PQA", "id": "XbGs_qK2PQA"}, {"title": "Eminem - Not Afraid (Official Video)", "link": "/watch?v=j5-yKhDd64s", "id": "j5-yKhDd64s"}, {"title": "Eminem - Mockingbird (Official Music Video)", "link": "/watch?v=S9bCLPwzSC0", "id": "S9bCLPwzSC0"}, {"title": "Eminem - When I'm Gone (Official Music Video)", "link": "/watch?v=1wYNFfgrXTI", "id": "1wYNFfgrXTI"}, {"title": "Eminem - Cleanin' Out My Closet (Official Video)", "link": "/watch?v=RQ9_TKayu9s", "id": "RQ9_TKayu9s"}, {"title": "Eminem - Till I Collapse [HD]", "link": "/watch?v=ytQ5CYE1VZw", "id": "ytQ5CYE1VZw"}, {"title": "Eminem - Love The Way You Lie ft. Rihanna", "link": "/watch?v=uelHwf8o7_U", "id": "uelHwf8o7_U"}]}
now, how can ı reach to “link” to open the song?
ı tried this but gives me error.
a = results.get("link")
webbrowser.open("www.youtube.com/" + "a")
which is;
AttributeError: 'str' object has no attribute 'get'
how can ı fix this problem? why results variable is str object why is not dictionary? where is my mistake?
and also ı am working on Ubuntu 19.10
The answer is because is a string not a dictionary. You need to convert it to a dictionary to use the get method.
Here is some simple code to get started:
from youtube_search import YoutubeSearch
import json
results = YoutubeSearch('eminem', max_results=10).to_json()
results_dict = json.loads(results)
Now, you are going to return 10 results and you want the links for each song (or channel).
for v in results_dict['videos']:
print('https://www.youtube.com' + v['link'])
If you want to see more, use the type
function:
type(results)
and type(results_dict)
results in different output.
You are converting the results to a JSON string. Don’t do that. Instead, call to_dict()
to get the results as a Python dictionary:
results = YoutubeSearch('eminem', max_results=10).to_dict()
for v in results:
print('https://www.youtube.com' + v['link'])
Note:
In a more recent version as of August 2022, the 'link']
key is changed to 'url_suffix'
.