Loading...

PHP網路爬蟲之抓取Open Graph(OG) Meta tags

Temperature: 0 °C

ChungChung
author_tools

用google爬了很多文章....

最後終於找到一篇內容.....

只是測試了半天...感覺奇怪為何內網抓的到..卻無法抓到外網的資料

搞了半天..結果是補夢網虛擬主機不能爬外網的資料.....

唉..國外的空間雖然溝通比較麻煩...但限制沒那麼多...

可以想玩什麼功能就可以測試...也便宜多了........雖然國內方便...可是服務還是有差......

把範例紀錄下來

首先是php code


function get_tags($url) {

$html = file_get_contents($url);

@libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$query = '//*/meta[starts-with(@property, \'og:\')]';
$result = $xpath->query($query);

foreach ($result as $meta) {
$property = $meta->getAttribute('property');
$content = $meta->getAttribute('content');

// replace og
$property = str_replace('og:', '', $property);
$list[$property] = $content;
}
return $list;
}

接著是使用方式

$url = 'http://www.example.com/';
$meta = get_tags($url);

echo $meta['locale'];
echo $meta['type'];
echo $meta['title'];
echo $meta['description'];
echo $meta['url'];
echo $meta['site_name'];
echo $meta['image'];


原文連結 http://9bugs.in/get-facebook-open-graphog-meta-tags-php-234

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

Prev
 PHP網路爬蟲之抓取meta_value

Next
Google_Nexus_6 

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

Responsive Web Design使用CSS3的Media Queries進行自適應網頁設計

Responsive Web...

智慧型手持裝置(手機、平板)已是非常普及的日常生活必需品。 所以現在,在開發系統時客戶都會詢問是否支...

PHP網路爬蟲之抓取meta value

PHP網路爬蟲之抓取meta...

最近在研究著如何抓取網頁中的meta值... php中的getmetatags函式可以抓取meta ...

讓圖片上傳前先產生預覽

讓圖片上傳前先產生預覽

專案需要,上傳圖片至網站時先產生預覽圖~ 參考了stackoverflow.com裡討論的的方式

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