這兩天在學下采集,使用querylist,使用起來很是簡單php
採集博客園博客代碼html
<?php namespace app\index\controller; use QL\QueryList; class Index { public function index() { $article=[]; for($i=1;$i<=13;$i++){ $ql = QueryList::get('https://www.cnblogs.com/jcydd/default.html?page='.$i); // 採集文章連接 $rt= $ql->find('.postTitle a')->attrs('href'); //halt($rt); foreach($rt as $k=>$v){ $ql2=QueryList::get($v); $article[$i.$k]['title']=$ql2->find('#cb_post_title_url')->text(); $article[$i.$k]['date']=$ql2->find('#post-date')->text(); $article[$i.$k]['date']=strtotime($article[$i.$k]['date']); $content=$ql2->find('#cnblogs_post_body')->html(); //獲取文章圖片連接 $img=$ql2->find('#topics img')->attrs('src'); //若是文章有圖片,則替換圖片本地地址 if(count($img)==0){ $article[$i.$k]['content']=$content; }else{ foreach($img as $vv){ $newimgurl=getimg($vv); $content=str_replace($vv,$newimgurl,$content); } $article[$i.$k]['content']=$content; } } } dump($article); } }