简体   繁体   English

如何获取Jsoup的属性内容?

[英]How to get attribute content Jsoup?

I have 我有

 <meta itemprop="datePublished" content="2015-01-26 12:37:00">

and I want to select the content . 我想选择内容 I try without success: 我尝试没有成功:

Document doc = Jsoup.connect("http://www.somesite.com/index.html").get();
Element link= doc.select("meta").first(); 
String contetn= link.attr("content");

But in my html I have: 但是在我的html中,我有:

<div style="overflow: visible;" itemscope="" itemtype="http://schema.org/Article">
<meta itemprop="url" content="http://www.somesite.com/index.html">
<meta itemprop="headline" content="some text">
<meta itemprop="datePublished" content="2015-01-26 12:37:00">
<meta itemprop="dateModified" content="2015-01-26 14:03:16">

You can see that I search for the 3-td tag meta and I can't select it. 您会看到我搜索了3-td标签元数据,但无法选择它。

Element link= doc.select("meta").first(); 

This will select only the first meta -element found; 这将仅选择找到的第一个meta元素。 since you have more than one in your second html, you'll get the wrong result. 由于第二个html中包含多个,因此您将得到错误的结果。

But here's an example : 但这是一个例子

final String html = "<div style=\"overflow: visible;\" itemscope=\"\" itemtype=\"http://schema.org/Article\">\n"
        + "<meta itemprop=\"url\" content=\"http://www.somesite.com/index.html\">\n"
        + "<meta itemprop=\"headline\" content=\"some text\">\n"
        + "<meta itemprop=\"datePublished\" content=\"2015-01-26 12:37:00\">\n"
        + "<meta itemprop=\"dateModified\" content=\"2015-01-26 14:03:16\">";

Document doc = Jsoup.parse(html);

Element meta = doc.select("meta[itemprop=datePublished]").first();
String content = meta.attr("content");

System.out.println(content);

Output: 2015-01-26 12:37:00 输出: 2015-01-26 12:37:00

This will select all meta -elements with attribute itemprop and attribute value datePublished . 这将选择所有具有属性itemprop和属性值datePublished meta元素。 From all found, just the first is taken. 从所有找到的,只采取第一个。 Finally from the single element you can get the value of the content -attribute. 最后,您可以从单个元素获取content -attribute的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM