如在程序員還看帶廣告的小說中所述,很多小說網(wǎng)站基本都有特別煩人的廣告,要么在整體div添加鏈接,誤觸就會跳轉(zhuǎn)到一些網(wǎng)站甚至是死循環(huán),某些手機app也是廣告很多,本文就將其應(yīng)用到laravel框架,最好先了解上一篇,隨后自行部署就可以了。
一、在laravel引入第三方類
1.在項目根目錄下app目錄中新建一個文件夾命名為Lib(自定義名稱)
2.如果引入第三方庫多的話可以在Lib下再新建幾個目錄分類,由于只引入了一個類,這里沒有新建文件夾。(根據(jù)引入類的多少自己定義)
將simple_html_dom.php復制到Lib下
3.找到項目根目錄下的composer.json文件,將第三方類的路勁寫入autoload下的classmap中,這樣才能自動加載
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Lib/simple_html_dom.php"
]
},
4.在cmd控制臺中切換到項目根目錄,執(zhí)行命令:
composer dumpautoload
5.在控制器中use這個類即可
use simple_html_dom;
$html = new simple_html_dom(); 使用
二、創(chuàng)建路由
Route::get('/novel_list','indexSpnovel@index');
三、創(chuàng)建控制器Spnovel.php
<?php namespace AppHttpControllersindex; use simple_html_dom; use IlluminateHttpRequest; use AppHttpControllersController; class Spnovel extends Controller { public function index(){ $url = "https://www.7kzw.com/85/85445/"; $list_html = mySpClass::getCurl($url); $data['List'] = self::getList($list_html); return view('index.spnovel.index',$data); } private static function getList($list_html){ $html = new simple_html_dom(); @$html->load($list_html); $list = $html->find('#list dd a'); foreach ($list as $k=>$v) { $arr1=$arr2=[]; $p1 = '/<a .*?>(.*?)</a>/i'; $p2 = '/<a .*? href="(.*?)">.*?</a>/i'; preg_match($p1,$v->outertext,$arr1); preg_match($p2,$v->outertext,$arr2); $content[$k][0]=$arr1[1]; $content[$k][1]=$arr2[1]; } array_splice($content,0,12); return $content; } } class mySpClass{ // 向服務(wù)器發(fā)送最簡單的get請求 public static function getCurl($url,$header=null){ // 1.初始化 $ch = curl_init($url); //請求的地址 // 2.設(shè)置選項 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//獲取的信息以字符串返回,而不是直接輸出(必須) curl_setopt($ch,CURLOPT_TIMEOUT,10);//超時時間(必須) curl_setopt($ch, CURLOPT_HEADER,0);// 啟用時會將頭文件的信息作為數(shù)據(jù)流輸出。 //參數(shù)為1表示輸出信息頭,為0表示不輸出 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); //不驗證證書 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //不驗證證書 if(!empty($header)){ curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//設(shè)置頭信息 }else{ $_head = [ 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0' ]; curl_setopt($ch,CURLOPT_HTTPHEADER,$_head); } // 3.執(zhí)行 $res = curl_exec($ch); // 4.關(guān)閉 curl_close($ch); return $res; } }
以上代碼的解釋:首先要對laravel框架了解,對php類要有所了解
訪問了以上路由,運行的是Spnovel.php控制器中的index方法,$url是某一本小說的章節(jié)列表的地址,將其作為參數(shù)運行自定義類mySpClass中的getcurl方法,返回這個頁面的html文檔字符串。運行此類中的getList方法,參數(shù)是需要解析的html字符串。將這個方法私有化,使用simple_html_dom解析,配置正則提取出每章的url地址和章節(jié)名稱。并返回這個數(shù)組,通過return view('index.spnovel.index',$data);將打開index/spnovel/index.blade.php,請看index.blade.php
四、創(chuàng)建視圖index.blade.php
<!DOCTYPE html> <html> <head> <title>爬取的小說列表</title> <style type="text/css"> body{padding:0px;margin:0px;} #lists{width:100%;padding:30px 50px;box-sizing:border-box;} ul{margin:0;padding: 0;overflow:hidden;} ul li{list-style:none;display:inline-block;float:left;width:25%;color:#444;} ul li:hover{color:#777;cursor: pointer;} img {z-index:-1;width:100%;height:100%;position:fixed;} </style> </head> <body> <img src="/static/img/index/novelbg.jpg"> <div id="lists"> <ul> @foreach($List as $item) <li> <a href="/novel_con{{$item[1]}}">{{$item[0]}}</a> </li> @endforeach </ul> </div> </body> </html>
以上代碼的解釋:css就簡單的寫到這里,img是作為背景圖片的。ul里面循環(huán)li,{{$item[1]}}是獲得的地址參數(shù),{{$item[0]}}是獲得的章節(jié)名稱。看一下數(shù)組和最后的效果。
五、運行
接下來就是每一章節(jié)的內(nèi)容了
先看路由:
Route::get('/novel_con/{a}//{c}','indexSpnovel@get_nContent');
這與每一章的url參數(shù)相對應(yīng),比如某一章的參數(shù)為:novel_con/85/85445/27248645.html
寫get_nContent方法:
public function get_nContent(Request $req){ $url1 = $req->a.'/'.$req->b.'/'.$req->c; $url = "https://www.7kzw.com/".$url1; $res = mySpClass::getCurl($url);//獲得 // 開始解析 $data['artic']= self::getContent($res); $next = (int)$req->c; $next = $next+1; $data['artic']['next']="/novel_con/".$req->a.'/'.$req->b.'/'.$next.'.html'; return view('index.spnovel.ncontent',$data); } private static function getContent($get_html){ $html = new simple_html_dom(); @$html->load($get_html); $h1 = $html->find('.bookname h1'); foreach ($h1 as $k=>$v) { $artic['title'] = $v->innertext; } // 查找小說的具體內(nèi)容 $divs = $html->find('#content'); foreach ($divs as $k=>$v) { $content = $v->innertext; } // 正則替換去除多余部分 $pattern = "/(<p>.*?</p>)|(<div .*?>.*?</div>)/"; $artic['content'] = preg_replace($pattern,'',$content); return $artic; }
解釋:$req->a,$req->b,$req->c,分別是三個參數(shù),然后將其合并為一個完整的請求某一章的地址,然后還是通過mySpClass::getCurl獲得某一章的html字符串。然后使用本類中的getContent解析這個頁面,先看解析方法,和上篇文章一章解析出章節(jié)的標題和內(nèi)容,寫到數(shù)組中,并且去掉了多余的文字廣告部分。$next則是存放的下一章的地址,用于在章節(jié)詳情頁面跳轉(zhuǎn)。
視圖ncontent.blade.php
<!DOCTYPE html> <html> <head> <title>{{$artic['title']}}</title> <style type="text/css"> h2{text-align:center;padding-top:30px;} div{margin:20px 50px;font-size:20px;} img {z-index:-1;width:100%;height:100%;position:fixed;} .next {position:fixed;right:10px;bottom:20px;background:coral;border-radius:3px;padding:4px;} .next:hover{color:#fff;} </style> </head> <body> <img src="/static/img/index/novelbg.jpg"> <h2>{{$artic['title']}}</h2> <a href="{{$artic['next']}}" class="next">下一章</a> <div> {!!$artic['content']!!} </div> </body> </html>
解釋:因為只有當前一篇所以不需要循環(huán),{{$artic['title']}}就是標題,也可以寫到title中。{!!$artic['content']!!}的寫法就是不需要轉(zhuǎn)義文章的內(nèi)容,否則就會有很多其他字符了,如<br>等。下一章的按鈕的地址直接就用傳遞過來的即可,position:fixed固定定位按鈕,隨時可以下一章。
運行:
總結(jié):本文最重要的環(huán)節(jié)就是引入第三方類,能夠應(yīng)用他,還有就是laravel的基礎(chǔ),比較習慣使用控制器視圖這種方式,帶模型的方式還請自行編寫驗證。
就對一本小說來說這就足夠了,當然我們可以擴充,將整站的小說列表寫出來,繼續(xù)傳合適的參數(shù)就更加完美了。