Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Resolving YouTube search links
#11
(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 Linux
Look 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'])
Output:
Learn Python - Full Course for Beginners [Tutorial] UC8butISFwT-Wl7EV0hUK0BQ
Quote
#12
Thanks for all the input everyone. These are some awesome solutions!
Quote
#13
(Aug-02-2020, 12:00 AM)snippsat Wrote:
Output:
Learn Python - Full Course for Beginners [Tutorial] UC8butISFwT-Wl7EV0hUK0BQ

Why does this only print a single result when how_many_hit = 10?
Quote
#14
(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.
Quote
#15
(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.
Quote
#16
(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'])
Output:
Learn Python - Full Course for Beginners [Tutorial] Python Tutorial - Python for Beginners [Full Course] What is Python? Why Python is So Popular? What Can You Do with Python? - The 3 Main Applications Python Tutorial for Beginners - Full Course in 11 Hours [2020] Учим Python за 1 час! #От Профессионала Python for Everybody - Full Course with Dr. Chuck Python超入門コース 合併版|Pythonの超基本的な部分をたった1時間で学べます【プログラミング初心者向け入門講座】 Lập Trình Cơ Bản PYTHON Tự Học Cho Người Mới Bắt Đầu Belajar Python [Dasar] - 01 - Apa Itu Python
pythonnewbie138 likes this post
Quote
#17
(Aug-04-2020, 04:17 PM)snippsat Wrote: for hit in hits['items']:
print(hit['snippet']['title'])[/python]

That makes sense. I tried removing the index but kept ['items'] in the print statement. Thank you so much for your help!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  webscrapping links and then enter those links to scrape data kirito85 2 699 Jun-13-2019, 02:23 AM
Last Post: kirito85

Forum Jump:


Users browsing this thread: 1 Guest(s)