使用 Jsoup 获取元标记值
Get meta tag value with Jsoup
我需要从所有元标记中获取一个值:
Elements elements= document.getElementsByTag("meta");
我会得到这些元素:
<meta name="twitter:image" content="https://lh3.googleusercontent.com/0daLqxVdiOGXvHI1R5aUFf_6znVlBHMturM9SXnpOQagADdYJDycyPzT-btcntR4jW4=w600-h300-pc0xffffff-pd">
<meta name="appstore:developer_url" content="http://www.verylittlenightmares.com">
<meta name="appstore:bundle_id" content="eu.bandainamcoent.verylittlenightmares">
<meta name="appstore:store_id" content="eu.bandainamcoent.verylittlenightmares">
<meta itemprop="price" content="200,00 $">
<meta itemprop="url" content="https://play.google.com/store/apps/details?id=eu.bandainamcoent.verylittlenightmares&rdid=eu.bandainamcoent.verylittlenightmares&feature=md&offerId">
我如何从该属性中获取值 (200,00 $)?
<meta itemprop="price" content="200,00 $">
private String getPrice(Document document) {
Elements elements= document.getElementsByTag("meta");
for (Element metaTag : elements) {
String content = metaTag.attr("content");
String itemprop = metaTag.attr("itemprop");
if ("price".equals(itemprop)) {
return content;
}
}
return null;
}
你可以这样做,不需要遍历所有 meta
元素,假设你只有一个节点 itemprop=price
:
Element meta = doc.select("meta[itemprop=price]").first();
System.out.println(meta.attr("content"));
输出为所需的 200,00 $'。
我需要从所有元标记中获取一个值:
Elements elements= document.getElementsByTag("meta");
我会得到这些元素:
<meta name="twitter:image" content="https://lh3.googleusercontent.com/0daLqxVdiOGXvHI1R5aUFf_6znVlBHMturM9SXnpOQagADdYJDycyPzT-btcntR4jW4=w600-h300-pc0xffffff-pd">
<meta name="appstore:developer_url" content="http://www.verylittlenightmares.com">
<meta name="appstore:bundle_id" content="eu.bandainamcoent.verylittlenightmares">
<meta name="appstore:store_id" content="eu.bandainamcoent.verylittlenightmares">
<meta itemprop="price" content="200,00 $">
<meta itemprop="url" content="https://play.google.com/store/apps/details?id=eu.bandainamcoent.verylittlenightmares&rdid=eu.bandainamcoent.verylittlenightmares&feature=md&offerId">
我如何从该属性中获取值 (200,00 $)?
<meta itemprop="price" content="200,00 $">
private String getPrice(Document document) {
Elements elements= document.getElementsByTag("meta");
for (Element metaTag : elements) {
String content = metaTag.attr("content");
String itemprop = metaTag.attr("itemprop");
if ("price".equals(itemprop)) {
return content;
}
}
return null;
}
你可以这样做,不需要遍历所有 meta
元素,假设你只有一个节点 itemprop=price
:
Element meta = doc.select("meta[itemprop=price]").first();
System.out.println(meta.attr("content"));
输出为所需的 200,00 $'。