Loading...

PHP網路爬蟲之抓取meta value

Temperature: 0 °C

ChungChung
author_tools

最近在研究著如何抓取網頁中的meta值...

php中的get_meta_tags函式可以抓取meta name 範例如下...


$tags = get_meta_tags('http://www.example.com/');
echo $tags['author']; // name
echo $tags['keywords']; // php documentation
echo $tags['description']; // a php manual
echo $tags['generator']; // 49.33;-86.59

但無法抓取 open graph 接著找到另一個範例寫著

$sites_html = file_get_contents('http://www.example.com/');

$html = new DOMDocument();
@$html->loadHTML($sites_html);
$meta_og_img = null;
$meta_og_title = null;
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
//If the property attribute of the meta tag is og:image
if($meta->getAttribute('property')=='og:image'){
//Assign the value from content attribute to $meta_og_img
$meta_og_img = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:title'){
//Assign the value from content attribute to $meta_og_title
$meta_og_title = $meta->getAttribute('content');
}
}
echo '<img src=' . $meta_og_img . ' /> <br/>';
echo $meta_og_title ;

但目前仍無法得到想要的結果...怪....

研究中....

https://innstory.com/story-PHP網路爬蟲之抓取meta_value-194
寫程式筆記

Prev
 石頭是台東的字是我寫的

Next
PHP網路爬蟲之抓取Open_GraphOG_Meta_tags 

About the Author

Chung

我是chung
網路工作者
主業是網站系統開發建置
副業是做夢,寫故事
作品請參考/teme.biz
做夢請參考/innstory.com
聯絡/chung.teme@gmail.com

#有人用筆寫日記,有人用歲月寫日記,有人用照片寫日記,而我,用innstory寫日記。

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

CSS Radio Button

CSS Radio Butt...

寫系統的時候三不五時總會用到Radio Button 和 Checkboxes 目前就在寫這個部份@...

黑客松Hackathon(二)

黑客松Hackathon(二...

這是一次不同的體驗... 我一直都渴望有所謂的工作團隊並且享受共同討論共同創作的樂趣... 這次在黑...

研究著vps的設定...

研究著vps的設定...

現在在研究著vps的設定... 上次發生怪客入侵事件後...原本已經把所有系統都重新檢查後移入國內空...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format