Resolving YouTube search links - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Resolving YouTube search links (/thread-28658.html) Pages:
1
2
|
RE: Resolving YouTube search links - snippsat - Aug-02-2020 (Aug-01-2020, 06:45 PM)pythonnewbie138 Wrote: I started looking into Selenium but I want the script to be OS-independent and Selenium only works on LinuxLook at web-scraping part-2 and search forum there is a lot info about this. (Aug-01-2020, 06:45 PM)pythonnewbie138 Wrote: I'll probably use a YouTube API wrapper when I get back to this.Can use YouTube API directly with Requests. This is the same as would enter a python in the search-box on youtube.com. import requests api_key = 'xxxyour-keyxxx' search = 'python' how_many_hit = 10 response = requests.get(f'https://www.googleapis.com/youtube/v3/search?part=snippet\ &maxResults={how_many_hit}&q={search}&type=video&key={api_key}') hits = response.json() print(hits['items'][0]['snippet']['title']) print(hits['items'][0]['snippet']['channelId'])
RE: Resolving YouTube search links - pythonnewbie138 - Aug-02-2020 Thanks for all the input everyone. These are some awesome solutions! RE: Resolving YouTube search links - pythonnewbie138 - Aug-02-2020 (Aug-02-2020, 12:00 AM)snippsat Wrote: Why does this only print a single result when how_many_hit = 10 ?
RE: Resolving YouTube search links - snippsat - Aug-02-2020 (Aug-02-2020, 07:26 PM)pythonnewbie138 Wrote: Why does this only print a single result when how_many_hit = 10?All 10 search hit is in hits = response.json() I did choose to take out first search hit as a demo,could of course have written a loop that printed out those values i wanted from all 10 hit. RE: Resolving YouTube search links - pythonnewbie138 - Aug-04-2020 (Aug-02-2020, 10:27 PM)snippsat Wrote: I did choose to take out first search hit as a demo,could of course have written a loop that printed out those values i wanted from all 10 hit. I'm confused on the syntax when putting this into a loop. I can create a loop just fine but the list index [0] is confusing me (probably because this is my first time working with json). Changing the index works to target a single search result but I'm unsure how to modify the syntax of the print statement to make it loop over the entire json instead of just a single index number.
RE: Resolving YouTube search links - snippsat - Aug-04-2020 (Aug-04-2020, 02:38 PM)pythonnewbie138 Wrote: but the list index [0] is confusing me (probably because this is my first time working with json)Back from .json() we are getting a Python dictionary,this usually contain both dictionaries and lists when coming from JSON.So [0] is used when access a element in that list.Using a loop then index assess can go away,as now looping over all items. import requests api_key = 'xxxxxx' search = 'python' how_many_hit = 10 response = requests.get(f'https://www.googleapis.com/youtube/v3/search?part=snippet\ &maxResults={how_many_hit}&q={search}&type=video&key={api_key}') hits = response.json() for hit in hits['items']: print(hit['snippet']['title'])
RE: Resolving YouTube search links - pythonnewbie138 - Aug-04-2020 (Aug-04-2020, 04:17 PM)snippsat Wrote: That makes sense. I tried removing the index but kept ['items'] in the print statement. Thank you so much for your help!
|