[問題] 抓不到YouTube搜尋結果的href

看板Python作者 (關你屁事)時間6年前 (2018/01/29 04:16), 6年前編輯推噓3(415)
留言10則, 5人參與, 6年前最新討論串1/1
大家好,寒假到了,無聊寫寫爬蟲 如題,我卡關了,不論我用get還是find都抓不到調皮的href,只會print標題而已 因為我只想抓第一個,所以我這樣寫,求各路大神幫忙(困擾我好幾天了都睡不好覺) https://i.imgur.com/k18flRu.jpg
https://i.imgur.com/0Hvg6u1.jpg
https://i.imgur.com/qYB59v1.jpg
from selenium import webdriver import time from bs4 import BeautifulSoup from se lenium.webdriver.common.keys import Keys browser=webdriver.Chrome() browser.impl icitly_wait(1) browser.get('https://www.youtube.com') time.sleep(5) URL="" for d ata in open('test.txt','r',encoding='UTF-8'): 胬? data=data.strip() 胬? br owser.get('https://www.youtube.com/results?search_query='+data+"+OP") 胬? sou p=BeautifulSoup(browser.page_source) 胬 time.sleep(2) 胬? for i in soup.f ind('a','yt-simple-endpoint style-scope ytd-video-renderer'): # 找尋第一個 <div> 區塊且 class="img_div_width" 胬胬胬? print (i) 胬胬胬? print ('-' *50) 胬胬胬? a = i.get('href') 胬胬胬? print (a) 胬胬胬? #U RL="https://www.youtube.com"+a 胬胬胬? #print (URL) 胬胬胬? print (-'*100) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.84.243 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1517170571.A.136.html ※ 編輯: s09173120901 (223.136.84.243), 01/29/2018 04:21:04

01/29 04:22, 6年前 , 1F
第二張圖註解有誤
01/29 04:22, 1F

01/29 11:57, 6年前 , 2F
find回傳的是<class 'bs4.element.Tag'> 不用再for
01/29 11:57, 2F

01/29 12:01, 6年前 , 3F
x = soup.find(....); x.get('href')即可
01/29 12:01, 3F

01/29 12:04, 6年前 , 4F
另外 想讓別人幫你找bug 弄個可以複製的code應該是基本的
01/29 12:04, 4F

01/29 12:59, 6年前 , 5F
youtube不是有api可以搜尋? 幹嘛那麼麻煩呀?
01/29 12:59, 5F
※ 編輯: s09173120901 (223.136.84.243), 01/30/2018 01:15:53

01/30 01:16, 6年前 , 6F
感謝提醒,已編輯
01/30 01:16, 6F
※ 編輯: s09173120901 (223.136.84.243), 01/30/2018 01:26:29

01/30 01:36, 6年前 , 7F
經過一番嘗試 終於理解二樓說的了,十分感謝
01/30 01:36, 7F

01/30 21:48, 6年前 , 8F
排版...
01/30 21:48, 8F

03/06 04:22, 6年前 , 9F
03/06 04:22, 9F

03/06 04:35, 6年前 , 10F
03/06 04:35, 10F
文章代碼(AID): #1QRY-B4s (Python)