硒从HREF属性获取ABS URL

发布于 2025-02-09 09:42:33 字数 237 浏览 2 评论 0原文

当我下载使用硒的页面并使用Java jsoup进行处理时。我在这样的源代码中获得了HREF:

< a href =“/f7-technical-trading” class =“ forumtitle”> gt; technology Trading</a>

是否有一种能够获得的方法来自此的绝对URL或迫使硒将其转换为绝对URL?获取页面后更新链接听起来并不像干净的解决方案。

when im downloading a page with selenium and process it with java jsoup. I get the hrefs in the source code like this:

<a href="/f7-technical-trading" class="forumtitle">Technical Trading</a>

Is there a way to get the absolute url from this or to force selenium to transform it to an absolute url? Updating the links after getting the page doesn't sound like a clean solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2025-02-16 09:42:33

如果您仅使用硒获得HREF,则可以按预期工作:

yourElement.get_attribute('href')

这是一个快速示例:

driver = webdriver.Chrome() # note this is my webdriver
driver.implicitly_wait(10)

url = "https://www.duckduckgo.co.uk"
driver.get(url)

aList = driver.find_elements(By.TAG_NAME, 'a')

for a in aList:
    print(a.get_attribute('href'))

输出包含:

https://duckduckgo.com/spread
https://duckduckgo.com/spread
https://duckduckgo.com/app
https://duckduckgo.com/app
https://duckduckgo.com/newsletter
https://duckduckgo.com/newsletter

这就是DOM外观的方式:(是相对的 - 但获取完整的路径)

If you get the href just with selenium, this works as expected:

yourElement.get_attribute('href')

This is a quick sample:

driver = webdriver.Chrome() # note this is my webdriver
driver.implicitly_wait(10)

url = "https://www.duckduckgo.co.uk"
driver.get(url)

aList = driver.find_elements(By.TAG_NAME, 'a')

for a in aList:
    print(a.get_attribute('href'))

Output contains:

https://duckduckgo.com/spread
https://duckduckgo.com/spread
https://duckduckgo.com/app
https://duckduckgo.com/app
https://duckduckgo.com/newsletter
https://duckduckgo.com/newsletter

This is how the DOM looks: (it's relative - but gets the full path)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文