-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing structured data (microdata) #163
Comments
Hey @eposjk
The parser looks good. But it supports only
Passing the DOMDocument in directly would be preferred for sure. I'll have a look around to see if I can find a way to extract it without re-parsing.
If a native solution is simple enough, I usually add it in directly. Here an external lib might make more sense. I guess the approach depends also the question if we can get the DOMDocument out of the packages used. Cheers, |
I've looked around and getting to a usable and maintainable approach seems to be tricky. My best approach atm would be something similar like the code below. It's placed in protected function createCrawlerFromContent(string $uri, string $content, string $type): ?Crawler
{
$previously = libxml_use_internal_errors(true);
$dom = (new HTML5)->loadHTML($content);
libxml_clear_errors();
libxml_use_internal_errors($previously);
// Get the base url.
$baseElements = $dom->getElementsByTagName('base');
// Crawler
$crawler = new Crawler(
null,
$uri,
$baseElements->count() > 0 ? $baseElements[0]->getAttribute('href') : $uri
);
$crawler->addDocument($dom);
return $crawler;
} But this keeps failing with an error in the base href tests... It might be cleaner to re-parse it after all. |
yusufkandemir/microdata-parser#4 This will cause issues if you want to do it with a DOM object. |
#16 proposes adding support for JSONLD.
There isn't only JSONLD - structured data can be provided also in the microdata notation, and: good news - there is a project which parses microdata and converts it to the same data structure as JSONLD: https://github.com/yusufkandemir/microdata-parser
So it should be possible to use both and treat it just like an additional JSONLD block!
A first test:
Internally this project uses an own DOM document class derived from DOMDocument. It has a function to import a DOMDocument - but Symphonys response class doesn't allow to access the DOMDocument.
I did a small test, but didn't fiddle it out how to pass the DOMDocument without reparsing - my try which didn't work:
What makes sense?
Adding separate PHPScraper functions for JSONLD and microdata? Or mixing both automatically? (my opinion: mixing)
How should support for microdata look like? Adding the other project to PHPScraper? Extending the existing classes or porting the whole functionality to PHPScraper?
The text was updated successfully, but these errors were encountered: