从具有属性的 JSoup <a> 标签中提取 Link
Extracting a Link from within a JSoup <a> Tag with Attributes
在 HTML 文档中,我有以下标签
<a id="myUniqueID" class="myClassName" href="/uniqueURLToExtract" data-original-title"">This is a Title</a>
我正在使用 JSoup 并想提取 myUniqueID
中包含的 href
中的 URL。阅读 SO
上的以下主题后
Jsoup - extracting data from an <a> tag, inside a <td> tag
JSoup parsing data from within a tag
我想到了这段代码
...
document = Jsoup.connect(mySiteAddress).timeout(10*1000).get();
...
String URLStr = document.select(a[id=myUniqueID]").text();
但这只是打印
This is a Title
我尝试过其他变体,但都没有成功。我要么没有打印任何内容,要么以空指针异常结束。
我不确定如何提取 <a>
标签中由 ID myUniqueID
标识的 URL。
尝试:
String URLStr = document.select("a[id=myUniqueID]").attr("href");
所以 attr()
超过 text()
。
您想获取 href
属性,而不是 a
的文本:
String href = document.getElementById("myUniqueID").attr("href");
在 HTML 文档中,我有以下标签
<a id="myUniqueID" class="myClassName" href="/uniqueURLToExtract" data-original-title"">This is a Title</a>
我正在使用 JSoup 并想提取 myUniqueID
中包含的 href
中的 URL。阅读 SO
Jsoup - extracting data from an <a> tag, inside a <td> tag
JSoup parsing data from within a tag
我想到了这段代码
...
document = Jsoup.connect(mySiteAddress).timeout(10*1000).get();
...
String URLStr = document.select(a[id=myUniqueID]").text();
但这只是打印
This is a Title
我尝试过其他变体,但都没有成功。我要么没有打印任何内容,要么以空指针异常结束。
我不确定如何提取 <a>
标签中由 ID myUniqueID
标识的 URL。
尝试:
String URLStr = document.select("a[id=myUniqueID]").attr("href");
所以 attr()
超过 text()
。
您想获取 href
属性,而不是 a
的文本:
String href = document.getElementById("myUniqueID").attr("href");