请问有人可以帮助我吗?我一直在到处搜索,但无法找到或产生正确的解决方案。我需要帮助从外部页面提取食谱数据。如果你看一下图像,你会注意到在同一页面上实现了一些 ld+json 标签,但我只需要提取配方数据并以 JSON 格式生成它,从那里,我知道如何将其加载到数据库中的表中。
我已经尝试过这个脚本,但不确定如何从页面中仅获取食谱数据。
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile('https://www.thecookingcat.com/recipes/fluffy-pancake.php');
libxml_clear_errors();
$recipe = array();
$xpath = new DOMXPath($dom);
$contentDiv = $dom->getElementById('content');
$recipe['title'] = $xpath->evaluate('string(div/h2/a)', $contentDiv);
foreach ($xpath->query('div/div/ul/li', $contentDiv) as $listNode) {
$recipe['ingredients'][] = $listNode->nodeValue;
}
print_r($recipe);
Run Code Online (Sandbox Code Playgroud)