- Chung
- 0°C
- 11 年又 40 天
PHP網路爬蟲之抓取Open GraphOG Meta tags
用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網路爬蟲之抓取OpenGraphOGMetatags-195
- 3,622
- 0
- 0

