私は、データをスクレイピングしてデータベースにデータを入れるアプリケーションを構築している初心者プログラマーです。
私は次のようなものをこすり取ろうとしています:
<meta property="og:image" content="image_url_1">
<meta property="og:image" content="image_url_2">
最初のメタ タグのコンテンツが必要ですが、2 番目のコンテンツは必要ありません。現在、$meta_og_image の値は 2 番目のメタ タグの内容です。これが私のphpコードです:
$html = new DOMDocument();
@$html->loadHTML($sites_html);
$meta_og_image = null; //reset
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('property')=='og:image'){
//Assign the value from content attribute to $meta_og_image
$meta_og_image = $meta->getAttribute('content');
}
}
echo $meta_og_image;
助けてくれてありがとう!