gpt4 book ai didi

java - 使用 Apache Tika 解析 HTML 问题

转载 作者:行者123 更新时间:2023-12-02 08:05:03 32 4
gpt4 key购买 nike

我正在爬行一个网页,爬行后从该网页提取所有链接,然后我尝试使用以下代码使用 Apache Tika 和 BoilerPipe 解析所有 url,因此对于某些 url,它解析得很好,但对于某些我收到这样的错误。它在 HTMLParser.java 上显示一些错误:第 102 行。这是 HTMLParser.java 中的第 102 行

String parsedText = tika.parseToString(htmlStream, md);

我还提供了 HTMLParse 代码。

org.apache.tika.exception.TikaException: TIKA-198: Illegal IOException from org.apache.tika.parser.microsoft.ooxml.OOXMLParser@67c28a6a
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:203)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:197)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:135)
at org.apache.tika.Tika.parseToString(Tika.java:357)
at edu.uci.ics.crawler4j.crawler.HTMLParser.parse(HTMLParser.java:102)
at edu.uci.ics.crawler4j.crawler.WebCrawler.handleHtml(WebCrawler.java:227)
at edu.uci.ics.crawler4j.crawler.WebCrawler.processPage(WebCrawler.java:299)
at edu.uci.ics.crawler4j.crawler.WebCrawler.run(WebCrawler.java:118)
at java.lang.Thread.run(Unknown Source)
Caused by: java.util.zip.ZipException: invalid block type
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:114)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:55)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:82)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:220)
at org.apache.poi.extractor.ExtractorFactory.createExtractor(ExtractorFactory.java:152)
at org.apache.tika.parser.microsoft.ooxml.OOXMLExtractorFactory.parse(OOXMLExtractorFactory.java:65)
at org.apache.tika.parser.microsoft.ooxml.OOXMLParser.parse(OOXMLParser.java:67)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:197)
... 8 more

这是我的 HTMLParser.java 文件 -

    public void parse(String htmlContent, String contextURL) {

InputStream htmlStream = null;
text = null;
title = null;
metaData = new HashMap<String, String>();

urls = new HashSet<String>();
char[] chars = htmlContent.toCharArray();

bulletParser.setCallback(textExtractor);
bulletParser.parse(chars);

try {
text = articleExtractor.getText(htmlContent);
} catch (BoilerpipeProcessingException e) {
e.printStackTrace();
}

if (text == null){
text = textExtractor.text.toString().trim();
}

title = textExtractor.title.toString().trim();
try {
Metadata md = new Metadata();
String utfHtmlContent = new String(htmlContent.getBytes(),"UTF-8");
htmlStream = new ByteArrayInputStream(utfHtmlContent.getBytes());
//The below line is at the line number 102 according to error above
String parsedText = tika.parseToString(htmlStream, md);
//very unlikely to happen
if (text == null){
text = parsedText.trim();
}
processMetaData(md);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(htmlStream);
}
bulletParser.setCallback(linkExtractor);
bulletParser.parse(chars);
Iterator<String> it = linkExtractor.urls.iterator();

String baseURL = linkExtractor.base();
if (baseURL != null) {
contextURL = baseURL;
}

int urlCount = 0;
while

(it.hasNext()) {
String href = it.next();
href = href.trim();
if (href.length() == 0) {
continue;
}
String hrefWithoutProtocol = href.toLowerCase();
if (href.startsWith("http://")) {
hrefWithoutProtocol = href.substring(7);
}
if (hrefWithoutProtocol.indexOf("javascript:") < 0
&& hrefWithoutProtocol.indexOf("@") < 0) {
URL url = URLCanonicalizer.getCanonicalURL(href, contextURL);
if (url != null) {
urls.add(url.toExternalForm());
urlCount++;
if (urlCount > MAX_OUT_LINKS) {
break;
}
}
}
}
}

如有任何建议,我们将不胜感激。

最佳答案

听起来像是格式错误的 OOXML 文档(.docx、.xlsx 等)。要检查最新的Tika版本是否仍然出现该问题,您可以download tika-app jar 并像这样运行它:

java -jar tika-app-1.0.jar --text http://url.of.the/troublesome/document.docx

这应该打印出文档中包含的文本。如果不起作用,请提交bug report包含有问题的文档的 URL(如果该文档不公开,则附上该文档)。

关于java - 使用 Apache Tika 解析 HTML 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8319604/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com