在python中使用硒获取所有href链接 [英] Fetch all href link using selenium in python

查看:128
本文介绍了在python中使用硒获取所有href链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python练习Selenium,我想使用Selenium来获取网页上的所有链接.

I am practicing Selenium in Python and I wanted to fetch all the links on a web page using Selenium.

例如,我希望 http://psychoticelites上所有<a>标签的href=属性中的所有链接.com/

For example, I want all the links in the href= property of all the <a> tags on http://psychoticelites.com/

我已经编写了一个脚本,并且可以正常工作.但是,它给了我对象地址.我尝试使用id标记获取值,但是,它不起作用.

I've written a script and it is working. But, it's giving me the object address. I've tried using the id tag to get the value, but, it doesn't work.

我当前的脚本:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox()
driver.get("http://psychoticelites.com/")

assert "Psychotic" in driver.title

continue_link = driver.find_element_by_tag_name('a')
elem = driver.find_elements_by_xpath("//*[@href]")
#x = str(continue_link)
#print(continue_link)
print(elem)

推荐答案

嗯,您只需要遍历列表即可:

Well, you have to simply loop through the list:

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem.get_attribute("href"))

find_elements_by_*返回元素列表(注意"elements"的拼写).遍历列表,获取每个元素并从中获取所需的所需属性值(在本例中为href).

find_elements_by_* returns a list of elements (note the spelling of 'elements'). Loop through the list, take each element and fetch the required attribute value you want from it (in this case href).

这篇关于在python中使用硒获取所有href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆