繁体   English   中英

如何使用Jsoup在html中添加所有元素?

[英]How can i add all elements in html with Jsoup?

File input = new File("1727209867.htm");
Document doc = Jsoup.parse(input, "UTF-8","http://www.facebook.com/people/Alison-Vella/1727209867");

我正在尝试解析此html文件,该文件已保存并在本地系统中使用。 但是解析不会解析所有的html。 因此,我无法获得所需的信息。 使用此代码,解析仅适用于6k char,但实际上html文件具有60k char。

这在jsoup中是不可能的,但是有一种解决方法

final File input = new File("example.html");
final int maxLength = 6000; // Limit of char's to read

InputStream is = new FileInputStream(input); // Open file for reading
StringBuilder sb = new StringBuilder(maxLength); // Init the "buffer" with the size required
int count = 0; // Count of chars readen
int c; // Char for reading

while( ( c = is.read() ) != -1 && count < maxLength ) // Read a single char until limit is reached
{
    sb.append((char) c); // Save the char into the buffer
    count++; // increment the chars readen
}


Document doc = Jsoup.parse(sb.toString()); // Parse the Html from buffer

解释:

  1. 逐字符读取文件到缓冲区,直到达到限制
  2. 从缓冲区解析文本并使用jsoup处理

问题:这不会关掉标签等问题-如果您处于极限状态,它将完全停止读取。

(可能) 解决方案:

  • 忽略它并停在您要的位置,对此进行解析并“ 修复 ”,或放下悬挂的html
  • 如果最后,请阅读直到到达下一个结束标记或> char
  • 如果您末了,请阅读直至到达下一个标记
  • 如果您末了,请阅读直到特定的标签或评论为止

暂无
暂无

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

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