当前位置:首页 » 好文分享 » 详情

PHP抓取网页mate内容代码

今天给大家带来的是php获取网页mate内容的代码
(标题,关键词,描述)

代码如下:
<?php
$site = "https://www.blogs.hk";
$content = get_sitemeta($site);
print_r($content);
/** 获取META信息 */
function get_sitemeta($url) {
$data = file_get_contents($url);
$meta = array();
if (!empty($data)) {
#Title
preg_match('/<TITLE>([\w\W]*?)<\/TITLE>/si', $data, $matches);
if (!empty($matches[1])) {
$meta['title'] = $matches[1];
}
#Keywords
preg_match('/<META\s+name="keywords"\s+content="([\w\W]*?)"/si', $data, $matches);
if (empty($matches[1])) {
preg_match("/<META\s+name='keywords'\s+content='([\w\W]*?)'/si", $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+content="([\w\W]*?)"\s+name="keywords"/si', $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+http-equiv="keywords"\s+content="([\w\W]*?)"/si', $data, $matches);
}
if (!empty($matches[1])) {
$meta['keywords'] = $matches[1];
}
#Description
preg_match('/<META\s+name="description"\s+content="([\w\W]*?)"/si', $data, $matches);
if (empty($matches[1])) {
preg_match("/<META\s+name='description'\s+content='([\w\W]*?)'/si", $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+content="([\w\W]*?)"\s+name="description"/si', $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+http-equiv="description"\s+content="([\w\W]*?)"/si', $data, $matches);
}
if (!empty($matches[1])) {
$meta['description'] = $matches[1];
}
}
return $meta;
}
?>

打赏
X
打赏方式:
  • 支付宝
  • 微信
  • QQ红包

打开支付宝扫一扫
日期:2017年10月23日 11:50:46 星期一   分类:好文分享   浏览(43547)
本文地址:https://www.blogs.hk/post-2759.html   [百度已收录]
声明:本页信息由网友自行发布或来源于网络,真实性、合法性由发布人负责,请仔细甄别!本站只为传递信息,我们不做任何双方证明,也不承担任何法律责任。文章内容若侵犯你的权益,请联系本站删除!

留言咨询

自动获取QQ

昵称

邮箱

网址

      Copyright © 2024 博客之家 版权所有  
      關於本站免責聲明sitemap新站登錄