使用Jsoup解析html和javascript

Parse html and javascript using Jsoup

本文关键字:javascript html 解析 Jsoup 使用      更新时间:2023-09-26

我正在解析包含HTML标签和javascript标签的HTML字符串,使用以下

public Document parse(String content) {
    return Jsoup.parse(content, "", Parser.xmlParser());
  }

问题是javascript元素只包含在一行中。

同样,我也尝试过

public Document parse(String content) {
    return Jsoup.parse(content, "", Parser.htmlParser());
  }

,这在Javascript中工作得很好…但是HTML元素没有结束标记。例:

<link rel="shortcut icon" href="../../static/public/img/favicon.ico" data-th-remove="all"></link>

被解析为

<link rel="shortcut icon" href="../../static/public/img/favicon.ico" data-th-remove="all">

当我运行我的应用程序时,它不起作用。

我怎样才能解决这个问题?是否有任何方法来解析HTML和Javascript一起使用JSOUP?

注意:我刚刚在JSOUP gitHub上创建了以下问题https://github.com/jhy/jsoup/issues/774

认为,

link元素在HTML中没有结束标记。它只出现在标题中。详见https://developer.mozilla.org/de/docs/Web/HTML/Element/link

所以当你使用Parser.htmlParser()

时,JSoup的行为与预期一致

你能更详细地解释一下,为什么你不能处理一个未关闭的link标签?