0

如何从 url 中提取文本?在我的代码中,它正在提取该网址的源代码......

DefaultHttpClient client = null;
client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials("test", "test"));
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);                
HttpGet request = new HttpGet("http://somehost.com");        
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();

Tika t = new Tika();
Metadata md = new Metadata();
Reader r = t.parse(content, md);
System.out.println(md);
System.out.println("Yes1: " +md.get("keywords"));
System.out.println("Yes2: " +md.get("title"));
System.out.println("Yes3: " +md.get("authors"));

//This gives the source code of that url not the actual content...
String ss= t.parseToString(content);
System.out.println("Yes4: " +ss);

有什么建议么??

4

2 回答 2

1

BoilerpipeContentHandler允许您在没有标记的情况下提取正文内容。包含的命令行实用程序展示了如何在您的程序中使用它并测试各种格式。

于 2011-07-15T23:05:41.803 回答
1

正如我所读到的..您可以使用此代码对 tika 进行操作

 byte[] raw = content.getContent();
 ContentHandler handler = new BodyContentHandler();
 Metadata metadata = new Metadata();
 Parser parser = new AutoDetectParser();
 parser.parse(new ByteArrayInputStream(raw), handler, metadata, new ParseContext());
 LOG.info("content: " + handler.toString());

即使我对其进行了测试,但我发现 handler.toString() 是空的!

于 2012-02-14T08:52:46.807 回答