从代码中获取所有href

Roo*_*kie 1 python selenium lxml beautifulsoup web-crawler

我正在制作一个网络爬虫.为了在页面中查找链接,我在selenium中使用xpath

driver = webdriver.Firefox()
driver.get(side)
Listlinker = driver.find_elements_by_xpath("//a")
Run Code Online (Sandbox Code Playgroud)

这很好.然而,测试爬虫,我发现并非所有链接都在a标签下.href有时也用在area或div标签中.

现在我被困住了

driver = webdriver.Firefox()
driver.get(side)
Listlinkera = driver.find_elements_by_xpath("//a")
Listlinkerdiv = driver.find_elements_by_xpath("//div")
Listlinkerarea = driver.find_elements_by_xpath("//area")
Run Code Online (Sandbox Code Playgroud)

这真的把爬行放在网络爬虫里.

我尝试过xpath "//@href",但这不起作用.我也尝试了几种方法来获得所有href url的有效方式,使用美丽的汤和lxml,但到目前为止,无济于事.对不起,我没有任何代码可以用美丽的汤和lxml显示我的努力,但由于这些被证明无用,我删除了它们,这不是最聪明的做法,我知道.我现在开始挽救这些不成功的尝试,为了我自己,如果我想再试一次,并想知道第一次出了什么问题

我能得到的任何帮助都将非常感激.

Sur*_*rya 6

试试这个:

ListlinkerHref = driver.find_elements_by_xpath("//*[@href]")
Run Code Online (Sandbox Code Playgroud)

  • 我无法用语言表达,你的建议所带来的紧张情绪缓解 - 非常感谢你! (2认同)