Extract the text from URLs using TIKA
Is it possible to extract text from URLs with Tika? Any links will be appreciated. Or TIKA is usable on开发者_开发问答ly for pdf, word and any other media documents?
Check the documentation - yes you can.
Example
java -jar tika-app-0.9.jar -t http://stackoverflow.com/questions/6656849/extract-the-text-from-url-using-tika
will show you the text on this page.
This is from lucid:
InputStream input = new FileInputStream(new File(resourceLocation));
ContentHandler textHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
PDFParser parser = new PDFParser();
parser.parse(input, textHandler, metadata);
input.close();
out.println("Title: " + metadata.get("title"));
out.println("Author: " + metadata.get("Author"));
out.println("content: " + textHandler.toString());
Instead of creating a PDFParser
you can use Tika's AutoDetectParser
to automatically process diff types of files:
Parser parser = new AutoDetectParser();
Yes, you can do that. Here is the code. This code uses apache http client
HttpGet httpget = new HttpGet("http://url.here");
HttpEntity entity = null;
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpget);
entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
parser.parse( instream, handler, metadata, new ParseContext());
String plainText = handler.toString();
FileWriter writer = new FileWriter( "/scratch/cache/output.txt");
writer.write( plainText );
writer.close();
System.out.println( "done");
}
to extract content from URL not from local file use this code:
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());
精彩评论