微信文章頁標題、內容、發佈時間、做者等信息。php
https://mp.weixin.qq.com/s?src=11×tamp=1523173327&ver=803&signature=6PCxJ*3ojH2ZM8pm56Lquward0mQMwSkPnqCvYlrDkQmL2kAEjGcFJMj2lzvpHyuyT30lczb2Ld0npUWmp*2Gj7bPJY3SCWrpRKlXJA0p4eQWPpAzMPJVmxPcRV5TtLS&new=1
利用瀏覽器開發者工具來分析待採集的區域選擇器,這裏不作詳細講解,若有不明白請自行了解jQuery選擇器和CSS選擇器相關知識,如圖:
html
分析結果:瀏覽器
.rich_media_title
#post-date
#meta_content>.rich_media_meta:eq(2)
.rich_media_content
選擇器寫法並非惟一的,只要能選擇到內容選擇器隨便怎麼寫均可以。微信
選擇器分析出來後代碼就很容易實現了。composer
composer require jaeger/querylist
<?php require 'vendor/autoload.php'; use QL\QueryList; $url = 'https://mp.weixin.qq.com/s?src=11×tamp=1523173327&ver=803&signature=6PCxJ*3ojH2ZM8pm56Lquward0mQMwSkPnqCvYlrDkQmL2kAEjGcFJMj2lzvpHyuyT30lczb2Ld0npUWmp*2Gj7bPJY3SCWrpRKlXJA0p4eQWPpAzMPJVmxPcRV5TtLS&new=1'; // 採集規則 $rules = [ 'title' => ['.rich_media_title','text'], 'date' => ['#post-date','text'], 'author' => ['#meta_content>.rich_media_meta:eq(2)','text'], 'content' => ['.rich_media_content','html'] ]; $data = QueryList::get($url)->rules($rules)->query()->getData(); print_r($data->all());
能夠很輕鬆的就寫出採集代碼出來,來看看運行結果:工具
Array ( [0] => Array ( [title] => âè´¸ææâæç«å°æªçèµ·ï¼ç¹ææ®å·²è¢«å大åªä½ç®è½°ä¸¨å¤åªè¯´ [date] => 2018-04-08 [author] => åè¯å [content] => <section class="xmteditor" style="display:none;" data-tools="æ°åªä½ç®¡å®¶" data-label="powered by xmt.cn"></section><p style="white-space: normal;"><img class="" data-ratio="0.134375" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/IqicDAGdXNibs5wrrbmbVJW8HZB9Qv5ajtuR4C4kIQI43GjtM0ZDsDWzFSCZ7UcthQ1bbPqBSENxEdvRyzzaBavg/640?wx_fmt=png" data-type="png" data-w="640" style="color: rgb(62, 62, 62);text-align: justify;line-height: 28.4444px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: auto !important;" width="auto"></p> <p style="white-space: normal;"><br></p> <p style="white-space: normal;"><span style="font-size: 15px;">èªç¹ææ®æèµ·âè´¸ææâ以æ¥ï¼ä¸æå©åå°±æ¬å¨å ¨çå¸åºç头ä¸ã</span></p> <p style="white-space: normal;"><br></p> <p style="white-space: normal;"><span style="font-size: 15px;">ä¸ç¾ä¸¤å½å ¬å¸çå¾ç¨ååè§æ¨¡è¶åè¶å¤§ï¼è¡å¸éè¡ï¼ä¸çç»æµçä¸ç¡®å®æ§ä¸æ¥ä¿±å¢ã</span></p> <p style="white-space: normal;"><br></p> ...... )
和預期同樣正確採集到了內容,可是內容亂碼了。post
解決亂碼的方法有不少,QueryList內置的解決方案是使用encoding()
和removeHead()
這個方法,但通過嘗試發現目前的場景都沒有效果,因此我使用了另外一種方法來解決微信亂碼問題,修改後的代碼以下:ui
<?php require 'vendor/autoload.php'; use Jaeger\GHttp; use QL\QueryList; $url = 'https://mp.weixin.qq.com/s?src=11×tamp=1523173327&ver=803&signature=6PCxJ*3ojH2ZM8pm56Lquward0mQMwSkPnqCvYlrDkQmL2kAEjGcFJMj2lzvpHyuyT30lczb2Ld0npUWmp*2Gj7bPJY3SCWrpRKlXJA0p4eQWPpAzMPJVmxPcRV5TtLS&new=1'; // 採集規則 $rules = [ 'title' => ['.rich_media_title','text'], 'date' => ['#post-date','text'], 'author' => ['#meta_content>.rich_media_meta:eq(2)','text'], 'content' => ['.rich_media_content','html'] ]; $html = GHttp::get($url); // 直接匹配出body中的內容 preg_match('/<body[^>]+>(.+)\s+<\/body>/s',$html,$arr); $html = $arr[0]; $data = QueryList::html($html)->rules($rules)->query()->getData(); print_r($data->all());
運行結果:url
Array ( [0] => Array ( [title] => 「貿易戰」戰火還沒有燃起,特朗普已被各大媒體炮轟丨外媒說 [date] => 2018-04-08 [author] => 雙語君 [content] => <section class="xmteditor" style="display:none;" data-tools="新媒體管家" data-label="powered by xmt.cn"></section><p style="white-space: normal;"><img class="" data-ratio="0.134375" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/IqicDAGdXNibs5wrrbmbVJW8HZB9Qv5ajtuR4C4kIQI43GjtM0ZDsDWzFSCZ7UcthQ1bbPqBSENxEdvRyzzaBavg/640?wx_fmt=png" data-type="png" data-w="640" style="color: rgb(62, 62, 62);text-align: justify;line-height: 28.4444px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: auto !important;" width="auto"></p> <p style="white-space: normal;"><br></p> <p style="white-space: normal;"><span style="font-size: 15px;">自特朗普挑起「貿易戰」以來,一把利劍就懸在全球市場的頭上。</span></p> <p style="white-space: normal;"><br></p> <p style="white-space: normal;"><span style="font-size: 15px;">中美兩國公佈的徵稅商品規模越變越大,股市震盪,世界經濟的不肯定性與日俱增。</span></p> <p style="white-space: normal;"><br></p> .... )
原文: http://study.querylist.cc/arc...