首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用jsoup库解析html meta标签

用jsoup库解析html meta标签
EN

Stack Overflow用户
提问于 2016-06-02 20:19:01
回答 4查看 12.7K关注 0票数 15

刚刚开始探索Jsoup库,因为我将在我的一个项目中使用它。我试着用谷歌搜索,但我找不到能帮助我的确切答案。这就是我的问题,我有一个带有meta标签的html文件,如下所示

代码语言:javascript
复制
<meta content="this is the title value" name="d.title">
<meta content="this is the description value" name="d.description">
<meta content="language is french" name="d.language">

和像这样的java pojo,

代码语言:javascript
复制
public class Example {
    private String title;
    private String description;
    private String language;

    public Example() {}

    // setters and getters go here
} 

现在我想解析html文件,提取d.title内容值,并将" content“的值存储在Example.title和d.description中,然后存储在Example.description中,依此类推。

我通过阅读jsoup食谱所做的事情是这样的,

代码语言:javascript
复制
Document doc = Jsoup.parse("test.html");
Elements metaTags = doc.getElementsByTag("meta");

for (Element metaTag : metaTags) {
    String content = metaTag.attr("content");
    String content = metaTag.attr("name");
}

这将遍历所有元标记,以获取其"content“和"name”属性的值,但我想要的是获取"content“属性的值,该属性的"name”属性为"d.title“,这样我就可以将其存储在Example.title中

更新:下面的 @P.J.Meisch answer实际上解决了这个问题,但我喜欢的代码太多了(我试图避免做完全相同的事情)。我是说,我在想,可以做一些像这样的事情

String title = metaTags.getContent("d.title")

其中d.title是"name“属性的值,这样可以减少代码行,我还没有找到这样的方法,但可能是因为我还不熟悉jsoup这就是为什么我问这个问题。但如果这样的方法不存在(如果它存在就好了,因为它让生活变得更容易),我会选择P.J.Meisch说。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-06-02 20:40:57

好的,所有的代码:

代码语言:javascript
复制
Document doc = Jsoup.parse("test.html");
Elements metaTags = doc.getElementsByTag("meta");

Example ex = new Example();

for (Element metaTag : metaTags) {
  String content = metaTag.attr("content");
  String name = metaTag.attr("name");

  if("d.title".equals(name)) {
    ex.setTitle(content);
  }
  if("d.description".equals(name)) {
    ex.setDescription(content);
  }
  if("d.language".equals(name)) {
    ex.setLanguage(content);
  }
}
票数 19
EN

Stack Overflow用户

发布于 2016-06-02 21:45:08

回答您最新的问题:这是不可能的,因为jsoup文档只反映了html文档的xml/dom结构。你必须迭代你自己的metas,但是你可以这样做:

代码语言:javascript
复制
Document doc = Jsoup.parse("test.html");

Map<String, String> metas = new HashMap<>();
Elements metaTags = doc.getElementsByTag("meta");

for (Element metaTag : metaTags) {
  String content = metaTag.attr("content");
  String name = metaTag.attr("name");
  metas.put(name, content);
}

Example ex = new Example();
ex.setTitle(metas.get("d.title"));
ex.setDescription(metas.get("d.description"));
ex.setLanguage(metas.get("d.language"));
票数 4
EN

Stack Overflow用户

发布于 2018-06-22 08:52:12

使用正确的特定库可以简化一切

查看我的用于解析meta标签内容的库

poshjosh/bcmetaselector

代码语言:javascript
复制
package com.bc.meta.selector;

import com.bc.meta.selector.htmlparser.AttributeContextHtmlparser;
import com.bc.meta.selector.util.SampleConfigPaths;
import com.bc.meta.ArticleMetaNames;
import com.bc.meta.impl.ArticleMetaNameIsMultiValue;
import java.util.Map;
import java.util.Iterator;
import java.util.function.BiFunction;
import org.json.simple.JSONValue;
import org.htmlparser.Parser;
import org.htmlparser.Node;
import org.htmlparser.Tag;

public class ReadMe {

    public static void main(String... args) throws Exception {

        final BiFunction<String, Node, String> nodeContentExtractor =
                (prop, node) -> node instanceof Tag ? ((Tag)node).getAttributeValue("content") : null;

        final SelectorBuilder<Node, String, Object> builder = Selector.builder();

        final Selector<Node> selector = builder.filter()
                .attributeContext(new AttributeContextHtmlparser(false))
                .configFilePaths(SampleConfigPaths.APP_ARTICLE_LIST)
                .jsonParser((reader) -> (Map)JSONValue.parse(reader))
                .propertyNames(ArticleMetaNames.values())
                .back()
                .multipleValueTest(new ArticleMetaNameIsMultiValue())
                .nodeConverter(nodeContentExtractor)
                .build();

        final Parser parser = new Parser();

        final String url = "https://edition.cnn.com/2018/06/21/africa/noura-hussein-asequals-intl/index.html";

        parser.setURL(url);

        Iterator<Node> nodes = parser.elements().iterator();

        final Map map = selector.selectAsMap(nodes, ArticleMetaNames.values());

        System.out.println("Printing meta tags data for: " + url + "\n" + map);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37591685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档