博客鏈接:http://www.charlesjiang.com/archives/9.html
背景:想做一個公眾號文章資源APP,發現搜狗有搜索公眾號文章功能,果斷開擼。http://wxiread.com (用CMS搭了個簡易的站)。
Step1.分析欄目及接口
搜狗微信地址:http://weixin.sogou.com/ , 如下圖:
搜狗分了20個欄目,分別是 熱門,推薦,段子手,養生堂,私房話… 對應地址從 /pcindex/pc/pc_0 到 /pcindex/pc/pc_19 , 如:http://weixin.sogou.com/pcindex/pc/pc_0/1.html [1.html為分頁號]。 整理欄目對應關系見表如下:
Step2.分析列表結構
文章列表頁由 li 節點構成,li 的ID可看做文章ID,li子節點包括文章標題,描述信息,作者,作者頭像等。
Step3.使用QueryList采集文章基本信息
QueryList 是一個基于PHP的DOM解析工具,功能強大,語法類似于JQuery;詳細使用可查看官方文檔
代碼如下:
protected function get_article_list($url)
{
//獲取文章LI ID規則
$rules = array(
'article_id' => array('li', 'id'),//文章ID
'inner_html' => array('li', 'html')
);
//遞歸獲取LI節點內容
$data = QueryList::Query($url, $rules)->getData(function($li) {
$id = $li['article_id'];
$info = QueryList::Query($li['inner_html'], array(
'article_url' => array(".wx-img-box > a", "href"), //文章地址
'author_url' => array(".pos-wxrw > a", "href"), //作者地址
'author_avatar' => array(".pos-wxrw > a > p > img", "src"), //作者頭像
'article_thumb' => array(".wx-img-box > a > img", "src"), //文章縮略圖
'author_name' => array(".pos-wxrw > a > p:eq(1)", "text"), //作者名稱
'article_title' => array(".wx-news-info2 > h4", "text"), //文章標題
'article_des' => array(".wx-news-info", "text"), //文章簡介
'article_create_at' => array(".wx-news-info2 [v]", "v"), //文章標題
'article_hits' => array(".wx-news-info2 > .s-p", "text", "", function($i){ preg_match('/\d+/', $i, $ms); return (int)$ms[0];}), //文章標題
))->data;
unset($info['inner_html']);
$info[0]['article_id'] = $id;
$info[0]['article_hits'] = intval($info[0]['article_hits']);
return $info[0];
});
return $data;
}
Step4.獲取文章詳情
第三步僅采集了文章基本信息(標題,作者,簡介等),要獲取詳情信息需要打開原文地址爬取內容。代碼如下:
/**
* 獲取文章詳情(并返回文章基本信息用于更新)
* @param $url
* @return array
*/
protected function get_content($url)
{
$option = array(
'http' => array(
'header' => "Referer:" . self::SET_REFER),
);
$url = file_get_contents($url, FALSE, stream_context_create($option));
//去除微信干擾元素!!!否則亂碼
$url = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $url);
$rules = array(
'content' => array('#js_content', 'html'),//文章內容
);
$content = QueryList::Query($url, $rules)->getData();
//原文鏈接
preg_match("/var msg_link = \".*\"/", $url, $matches);
$orUrl = html_entity_decode(urldecode($matches[0]));
$orUrl = substr(explode('var msg_link = "', $orUrl)[1], 0, -4);
//原文標題 !避免出現標題被截取
preg_match("/var msg_title = \".*\"/", $url, $matches);
$orTitle = $matches[0];
$orTitle = substr(explode('var msg_title = "', $orTitle)[1], 0, -1);
//原文作者頭像
preg_match("/var round_head_img = \".*\"/", $url, $matches);
$orAuthAvatar = $matches[0];
$orAuthAvatar = substr(explode('var round_head_img = "', $orAuthAvatar)[1], 0, -1);
//原文縮略圖
preg_match("/var msg_cdn_url = \".*\"/", $url, $matches);
$orImgUrl = $matches[0];
$orImgUrl = substr(explode('var msg_cdn_url = "', $orImgUrl)[1], 0, -1);
return array(
'content' => $content[0]['content'],
'article_url' => urldecode($orUrl),
'article_title' => html_entity_decode($orTitle),
'author_avatar' => $orAuthAvatar,
'article_thumb' => $orImgUrl
);
}
Step5.數據入庫
數據庫大致設計如下:
wechat_article: 保存文章基本信息
wechat_article_content: 文章詳情信息
wechat_category: 欄目信息
wechat_article_ids: 已被導入的文章,避免重復導入(可以選用Redis等)
Step6.將文章同步到CMS
方便操作,我選用的是PHPCMS, 在后臺建好欄目,寫一個導入腳本,用定時任務執行,現情況如下:
其他
1.微信圖片防盜鏈:
微信原文圖片做了防盜鏈,在同步到CMS時,我將所有圖片鏈接替換為中轉地址如:
http://www.wxiread.com/api.php?op=ref_control&url=http://mmbiz.qpic.cn/mmbiz_gif/jxateR9eXe1x9yPwA89Rm8mtjZYCgMuiauGKMMOtsEVAyCrsicJVhNv5ON4QOfLJHXRdsUyj8kklDwzicIrNSRyNw/0?wx_fmt=gif
api.php 代碼如下:
$sogouPre = "http://img02.store.sogou.com/net/a/05/link?appid=100520091&url=";
/**
* 防盜鏈處理
*/
$url = @trim($_REQUEST['url']);
if (empty($url) || !isUrl($url)) {
return;
}
$imgType = getImgType($url);
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Referer:http://weixin.sogou.com/ \n" .
"User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 \n".
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
)
);
$context = stream_context_create($opts);
$file = file_get_contents($sogouPre . $url, FALSE, $context);
header("Content-type:image/{$imgType}");
echo $file;