如何使用selenium提取元素(href)并保存到变量中

时间:2015-12-06 22:57:05

标签: python selenium

我希望在Youtube搜索中提取第一个链接的网址,而无需加载视频页面。

import urllib
from selenium import webdriver
song='burning bridges'
songsearch=urllib.parse.quote_plus(song)
driver = webdriver.Chrome()
driver.get('https://www.youtube.com/results?search_query=%s+lyrics' % songsearch)
urlinc = driver.find_element_by_partial_link_text(song)

print (urlinc)
print ('done')
driver.quit()

HREF =#&34; /观看V = R8tVZ_QfYe4&#34?; class =" yt-uix-sessionlink yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 spf-link"数据sessionlink =" itct = CB0Q3DAYACITCLqBt - IyMkCFVPRvgodYRkCZij0JFIWYnVybmluZyBicmlkZ2VzIGx5cmljcw" title =" OneRepublic-Burning Bridges(歌词)HD NATIVE"的rel =" SPF-预取"咏叹调-describedby ="描述-ID-231739" dir =" ltr"> OneRepublic-Burning Bridges(歌词)HD NATIVE

输出:

selenium.webdriver.remote.webelement.WebElement (session="9bf01a91206f82d9462b6803c5084aa5", element="0.22866418561898172-1")

done

1 个答案:

答案 0 :(得分:2)

你可以尝试:

>>> urlinc.get_attribute('href')
'https://www.youtube.com/watch?v=omhYPjksUy8'
>>>