小編給大家分享一下laravel中如何使用simple_html_dom爬取顯示整本小說,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
一、在laravel引入第三方類
1.在項目根目錄下app目錄中新建一個文件夾命名為Lib(自定義名稱)
2.如果引入第三方庫多的話可以在Lib下再新建幾個目錄分類,由于只引入了一個類,這里沒有新建文件夾。(根據引入類的多少自己定義)
將simple_html_dom.php復制到Lib下
3.找到項目根目錄下的composer.json文件,將第三方類的路勁寫入autoload下的classmap中,這樣才能自動加載
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Lib/simple_html_dom.php"
]
},
4.在cmd控制臺中切換到項目根目錄,執行命令:
composer dumpautoload
5.在控制器中use這個類即可
use simple_html_dom;
$html = new simple_html_dom(); 使用
二、創建路由
Route::get('/novel_list','index\Spnovel@index');三、創建控制器Spnovel.php
<?php
namespace App\Http\Controllers\index;
use simple_html_dom;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
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{
// 向服務器發送最簡單的get請求
public static function getCurl($url,$header=null){
// 1.初始化
$ch = curl_init($url); //請求的地址
// 2.設置選項
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//獲取的信息以字符串返回,而不是直接輸出(必須)
curl_setopt($ch,CURLOPT_TIMEOUT,10);//超時時間(必須)
curl_setopt($ch, CURLOPT_HEADER,0);// 啟用時會將頭文件的信息作為數據流輸出。
//參數為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);//設置頭信息
}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.執行
$res = curl_exec($ch);
// 4.關閉
curl_close($ch);
return $res;
}
}以上代碼的解釋:首先要對laravel框架了解,對php類要有所了解
訪問了以上路由,運行的是Spnovel.php控制器中的index方法,$url是某一本小說的章節列表的地址,將其作為參數運行自定義類mySpClass中的getcurl方法,返回這個頁面的html文檔字符串。運行此類中的getList方法,參數是需要解析的html字符串。將這個方法私有化,使用simple_html_dom解析,配置正則提取出每章的url地址和章節名稱。并返回這個數組,通過return view('index.spnovel.index',$data);將打開index/spnovel/index.blade.php,請看index.blade.php
四、創建視圖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里面循環li,{{$item[1]}}是獲得的地址參數,{{$item[0]}}是獲得的章節名稱??匆幌聰到M和最后的效果。

五、運行

接下來就是每一章節的內容了
先看路由:
Route::get('/novel_con/{a}//{c}','index\Spnovel@get_nContent');這與每一章的url參數相對應,比如某一章的參數為: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);
$h2 = $html->find('.bookname h2');
foreach ($h2 as $k=>$v) {
$artic['title'] = $v->innertext;
}
// 查找小說的具體內容
$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,分別是三個參數,然后將其合并為一個完整的請求某一章的地址,然后還是通過mySpClass::getCurl獲得某一章的html字符串。然后使用本類中的getContent解析這個頁面,先看解析方法,和上篇文章一章解析出章節的標題和內容,寫到數組中,并且去掉了多余的文字廣告部分。$next則是存放的下一章的地址,用于在章節詳情頁面跳轉。
視圖ncontent.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{$artic['title']}}</title>
<style type="text/css">
h3{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">
<h3>{{$artic['title']}}</h3>
<a href="{{$artic['next']}}" class="next">下一章</a>
<div>
{!!$artic['content']!!}
</div>
</body>
</html>解釋:因為只有當前一篇所以不需要循環,{{$artic['title']}}就是標題,也可以寫到title中。{!!$artic['content']!!}的寫法就是不需要轉義文章的內容,否則就會有很多其他字符了,如<br>等。下一章的按鈕的地址直接就用傳遞過來的即可,position:fixed固定定位按鈕,隨時可以下一章。
運行:

以上是“laravel中如何使用simple_html_dom爬取顯示整本小說”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。