Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

php - How do I scrape and combine multiple application/ld+json tags?

I'm writing a script to pull metadata out of submitted (external) URLs. I found a very helpful post that got me 90% of the way there, however it assumes there's only going to be one relevant script tag, and I need to parse pages that have multiple script tags.

How can I modify the below statement so that it combines all matching json script tags into one chunk of json?

$xp = new domxpath($d);
$jsonScripts = $xp->query( '//script[@type="application/ld+json"]' );
$json = trim( $jsonScripts->item(0)->nodeValue );

// decode the JSON string we find into a associative array
$data = json_decode( $json, true );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not 100% without testing the code, but $jsonScripts will contain multiple items if there is more than one, and the total number of these can be obtained using the length property.

To go through them all, use a simple for loop to obtain each one:

...
$jsonScripts = $xp->query( '//script[@type="application/ld+json"]' );
$json = '';
for ($i = 0; $i < $jsonScripts->length; $i++) {
    $json .= trim($jsonScripts->item($i)->nodeValue );
}
...

This will simply concatenate the contents of each script node onto the $json variable. I'm not certain whether you want to separate them by a delimiter or anything else.

Where your code was previously using:

$json = trim( $jsonScripts->item(0)->nodeValue );

This was obtaining only the first element. The code I suggested uses:

$json .= trim($jsonScripts->item($i)->nodeValue);

This concatenates the $ith element and should therefore output all of them starting with the 0th.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...