JSOUP 提取 href 标题
JSOUP extracting a href title
我有 HTML:
<h2 class="p-job-title">
<a href="/work/android-software" rel="nofollow"
title="work Android - Software Developer" class="job-offer ">
<strong class="keyword">Android</strong> - Software <strong class="keyword">Developer</strong>
</a>
</h2>
如何从 href 中提取标题 ("work Android - Software Developer")?我不需要 href,只需要标题。
您的操作与任何其他属性完全相同。您选择带有属性的标签,在您的情况下 <a ...>
与 href
和 title
类似
Elements hrefs = doc.select("a[href][title]");
然后您使用 tag.attr("attributeName")
从标签中选择属性
for (Element el : hrefs){
System.out.println(el.attr("title"));
}
顺便说一句,如果您确定只选择了一个元素,或者您想从第一个选择的标签中读取 title
,您可以调用 Elements
代表所选组的 attr
方法标签。
System.out.println(doc.select("a[href][title]").attr("title"));
我有 HTML:
<h2 class="p-job-title">
<a href="/work/android-software" rel="nofollow"
title="work Android - Software Developer" class="job-offer ">
<strong class="keyword">Android</strong> - Software <strong class="keyword">Developer</strong>
</a>
</h2>
如何从 href 中提取标题 ("work Android - Software Developer")?我不需要 href,只需要标题。
您的操作与任何其他属性完全相同。您选择带有属性的标签,在您的情况下 <a ...>
与 href
和 title
类似
Elements hrefs = doc.select("a[href][title]");
然后您使用 tag.attr("attributeName")
for (Element el : hrefs){
System.out.println(el.attr("title"));
}
顺便说一句,如果您确定只选择了一个元素,或者您想从第一个选择的标签中读取 title
,您可以调用 Elements
代表所选组的 attr
方法标签。
System.out.println(doc.select("a[href][title]").attr("title"));