【问题标题】:Why html code in chrome devtools and html code parsed by jsoup are different?为什么chrome devtools中的html代码和jsoup解析的html代码不一样?
【发布时间】:2019-11-20 17:16:36
【问题描述】:

我正在尝试从 HADOOP Jira 问题站点 (https://issues.apache.org/jira/projects/HADOOP/issues/HADOOP-16381?filter=allopenissues) 中提取有关问题创建日期的信息

正如您在这个Screenshot 中看到的,创建日期是时间标签之间的文本,其类是实时戳记(例如<time class=livestamp ...> 'this text' </time>

所以,我尝试用下面的代码解析它。

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CreatedDateExtractor {
    public static void main(String[] args) {
        String url = "https://issues.apache.org/jira/projects/HADOOP/issues/HADOOP-16381?filter=allopenissues";
        Document doc = null;

        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Elements elements = doc.select("time.livestamp"); //This line finds elements that matches time tags with livestamp class
        System.out.println("# of elements : "+ elements.size());
        for(Element e: elements) {
            System.out.println(e.text());
        }   
    }
}



我希望提取创建日期,但实际输出是 元素数:0

我发现这有点不对劲。因此,我尝试使用下面的代码从那一侧解析整个 html 代码。

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CreatedDateExtractor {
    public static void main(String[] args) {
        String url = "https://issues.apache.org/jira/projects/HADOOP/issues/HADOOP-16381?filter=allopenissues";
        Document doc = null;

        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Elements elements = doc.select("*"); //This line finds whole elements in html document.
        System.out.println("# of elements : "+ elements.size());
        for(Element e: elements) {
            System.out.println(e);
        }   
    }
}

我将chrome devtools中的html代码和我解析的html代码一一对比。然后我发现它们是不同的。

你能解释一下为什么会发生这种情况,并给我一些如何提取创建日期的建议吗?

【问题讨论】:

    标签: java html google-chrome-devtools jsoup html-parsing


    【解决方案1】:

    我建议你获取带有“time”标签的元素,并使用 select 来获取具有“livestamp”类的时间标签。示例如下:

    Elements timeTags = doc.select("time");
    Element timeLivestamp = null;
    for(Element tag:timeTags){
      Element livestamp = tag.selectFirst(".livestamp");
      if(livestamp != null){
       timeLivestamp = livestamp;
       break;
       }
    
    }
    

    我不知道为什么,但是当我想将 Jsoup 的 .select() 方法与多个选择器一起使用时(就像你使用的 time.livestamp 一样),我会得到这样有趣的输出。

    【讨论】:

    • 而且,我检查了页面,恕我直言,仅使用一个 .select() 查询来获取创建日期太难了。有很多带有“时间戳”类的元素。我建议你使用 Jsoup 的 getElementsByAttributeValue() 方法。例如,如果要获取 Created 日期时间,则必须使用 Element spanElem = doc.getElementsByAttributeValue("id","created-val").first() --> 获取 Created 跨度,然后使用 Element createdtimeElem = spanElem.selectFirst(".livestamp") 将正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2012-01-31
    • 2015-01-22
    • 2014-08-12
    • 2017-09-05
    • 2018-07-12
    • 1970-01-01
    相关资源
    最近更新 更多