作者s09173120901 (关你屁事)
看板Python
标题[问题] 抓不到YouTube搜寻结果的href
时间Mon Jan 29 04:16:08 2018
大家好,寒假到了,无聊写写爬虫
如题,我卡关了,不论我用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://webptt.com/cn.aspx?n=bbs/Python/M.1517170571.A.136.html
※ 编辑: s09173120901 (223.136.84.243), 01/29/2018 04:21:04
1F:→ s09173120901: 第二张图注解有误01/29 04:22
2F:推 ckc1ark: find回传的是<class 'bs4.element.Tag'> 不用再for01/29 11:57
3F:推 ckc1ark: x = soup.find(....); x.get('href')即可01/29 12:01
4F:推 ckc1ark: 另外 想让别人帮你找bug 弄个可以复制的code应该是基本的01/29 12:04
5F:推 tentenlee: youtube不是有api可以搜寻? 干嘛那麽麻烦呀?01/29 12:59
※ 编辑: s09173120901 (223.136.84.243), 01/30/2018 01:15:53
6F:→ s09173120901: 感谢提醒,已编辑01/30 01:16
※ 编辑: s09173120901 (223.136.84.243), 01/30/2018 01:26:29
7F:→ s09173120901: 经过一番尝试 终於理解二楼说的了,十分感谢 01/30 01:36
8F:嘘 jamesxxx1997: 排版... 01/30 21:48