如何从网址中提取文字?在我的代码中,它正在提取该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);
有什么建议吗?
答案 0 :(得分:1)
BoilerpipeContentHandler允许您在没有标记的情况下提取正文内容。附带的命令行实用程序显示了如何在程序中使用它并测试各种格式。
答案 1 :(得分: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()是空的!