无法返回 href (jSoup) 中的文本
这是我用来从下面的 html 片段访问“test”的代码片段。如何从 html 中访问网址 https://www.google.com ?
Elements e = doc.getElementsByAttribute("href");
Iterator<Element> href = e.iterator();
while ( href.hasNext() ){
Element link = href.next();
String text = link.text();
}
<a href="javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','')">Test</a>
Here is a code snippet I am using to access "test" from below html snippet. How can I access the URL https://www.google.com from within html ?
Elements e = doc.getElementsByAttribute("href");
Iterator<Element> href = e.iterator();
while ( href.hasNext() ){
Element link = href.next();
String text = link.text();
}
<a href="javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','')">Test</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不是 Jsoup 专家,但 Jsoup 是一个 html 解析器,你不能用它来解析
javascript
标记内的内容。因此,您的方法应该是提取
"javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','') ”
使用 Jsoup。
比使用 正则表达式 来获取内容/url。
I am no Jsoup expert but Jsoup is an html parser you can't use it to parse content inside
javascript
tag.So, your approach should be to extract
"javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','')"
using Jsoup.
Than use regular expressions to fetch the content/url.
HREF 是一个属性,您可以使用 Jsoup 元素的
attr
方法访问它。这将为您提供属性的全部内容,当然,您需要一些模式匹配来检索 URL。The HREF is an attribute which you can access with the
attr
method of Jsoup's element. This gives you the whole content of the attribute, of course, you need some pattern matching to retrieve the URL.