仅获取 id 和 href
我的输出由多个 id 和 href 以及其他数据组成。
如何使用 Htmlunit 和 Java 代码获取所有 id 和 href 值?
我需要强制获取所有 href 值,但 id 不是强制的...
我该怎么做以及我应该在哪里编写给定的代码。
<a id="Repeater1_ctl00_HyperLink2" href="/jobs/Physical-Security-Specialist-308550900.html">
<a id="Repeater1_ctl05_HyperLink1" href="/jobs/Safety-and-Occup-Hlth-Specialist-308549000.html">
<a id="Repeater1_ctl08_HyperLink2" href="/jobs/Nurse-Practitioner-Oca-Esep-Dh-308526400.html">
<a id="Repeater1_ctl09_HyperLink1" href="/jobs/Nurse-Practitioner-Oca-Esep-Dh-308526400.html">
ETC.....
My output consists of multipe id and href and other data.
How to get all the id and href values using Htmlunit and Java code?
I need to get all the href values mandatory but id is not mandatory...
How can I do that and where should I write the given code.
<a id="Repeater1_ctl00_HyperLink2" href="/jobs/Physical-Security-Specialist-308550900.html">
<a id="Repeater1_ctl05_HyperLink1" href="/jobs/Safety-and-Occup-Hlth-Specialist-308549000.html">
<a id="Repeater1_ctl08_HyperLink2" href="/jobs/Nurse-Practitioner-Oca-Esep-Dh-308526400.html">
<a id="Repeater1_ctl09_HyperLink1" href="/jobs/Nurse-Practitioner-Oca-Esep-Dh-308526400.html">
etc.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会为您编写代码:)但我可以为您提供解决方案的想法。
获取 Html 页面。尝试获取页面的所有 HtmlAnchor 对象(您可以使用 XPath 来实现)。迭代 HtmlAnchor 集合并执行方法 getHrefAttribute() 获取 href 属性。类似地,使用 HtmlAnchor 的 getId() 方法来获取其 id。就是这样 :)
I'm not writing code for you :) But I can give you an idea of the solution.
Get the HtmlPage. Try getting all the HtmlAnchor object of the page (you can use XPath for this). Iterate over the HtmlAnchor collection and execute the method getHrefAttribute() to get the href attribute. Similary use the getId() method of the HtmlAnchor to get its id. That's it :)