Skip to content
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

Open
eposjk opened this issue Dec 8, 2022 · 3 comments
Open

Parsing structured data (microdata) #163

eposjk opened this issue Dec 8, 2022 · 3 comments

Comments

@eposjk
Copy link

eposjk commented Dec 8, 2022

#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:

$jsonlddata = \YusufKandemir\MicrodataParser\Microdata::fromHTML($web->client->getResponse()->getContent(), $web->currentUrl())->toArray();

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:

$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->importNode($web->filterFirst('//*')->getNode(0), true);
$jsonlddata = \YusufKandemir\MicrodataParser\Microdata::fromDOMDocument($dom);

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?

@spekulatius
Copy link
Owner

Hey @eposjk

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!

The parser looks good. But it supports only ^8.1- that would require either a major release as it blocks 7.4 (will be dropped once it phased out a bit more) and 8.0 (still in use and security-supported). I've had a quick look over the lib and couldn't spot a reason why 8.0 wouldn't work...

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.

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.

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?

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,
Peter

@spekulatius
Copy link
Owner

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 GoutteClient.php (extension of \Goutte\Client like in the other PR) with a method replacing createCrawlerFromContent:

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.

@joshua-bn
Copy link

yusufkandemir/microdata-parser#4

This will cause issues if you want to do it with a DOM object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants