deepslam/content-parser
Laravel 5 package to extract a web page’s main content and title using automatic algorithms. Supports Graby (default) and Mercury API parsers, with an extensible architecture and optional HTML cleanup (remove CSS/style attrs, strip tags) for clean output.
With this package, you can easily detect main content on different web pages and grab the content from it. This package provides following features:
The package uses automatic algorithms for grabbing data from web pages. You'll receive the title and the content from needle web page.
The package requires follow solutions:
You can install the package via Composer. Just run:
php composer require deepslam/content-parser
Further, you have to add service provider in your config/app.php:
...
Deepslam\ContentParser\ContentParserServiceProvider::class,
...
At next step you need to create alias in your config/app.php:
'ContentParser' => Deepslam\ContentParser\ContentParser::class,
After it you need to publish configs:
php artisan vendor:publish --provider="Deepslam\ContentParser\ContentParserServiceProvider"
Do not forget to run config:cache command:
php artisan config:cache
That's all!
There are two different parsers:
Thus you have 3 configs:
You can easily use ContentParser:
$parser = ContentParser::create();
There will be ContentParser object created.
This configuration will use "Graby" parser. If you need to use another one, you can specify it as a parameter:
$parser = ContentParser::create('mercury');
As result, you will receive ContentParser object.
For parse data, you must use parse method which return true\false result (true if data has been received, false if not)
$parser->parse($url)
For getting a result of parsing there is one method:
There are a few methods in this object:
If you want to add a new parser you must create a new class and inherit it from \Deepslam\ContentParser\ContentParser class. You must realise the only one method - parse which must return bool as result and changes internal result object.
After it, you must specific your new class in the /config/deepslam/parser.php parsers array.
To use you parser specify it when you call ContentParser as shows below:
$parser = ContentParser::create('<your alias of parser>');
$parser = ContentParser::create('<parser which you need>');
$parser->parse('<url to grab>');
$result = $parser->getResult();
<your_model>->name = $result->getTitle();
<your_model>->description = $result->getContent();
If you find bug or have question\suggestion you can send e-mail to me: [[email protected]][email protected]
How can I help you explore Laravel packages today?