溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

laravel中如何使用simple_html_dom爬取顯示整本小說

發(fā)布時間:2021-01-06 10:47:58 來源:億速云 閱讀:210 作者:小新 欄目:編程語言

小編給大家分享一下laravel中如何使用simple_html_dom爬取顯示整本小說,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

一、在laravel引入第三方類

1.在項目根目錄下app目錄中新建一個文件夾命名為Lib(自定義名稱)

2.如果引入第三方庫多的話可以在Lib下再新建幾個目錄分類,由于只引入了一個類,這里沒有新建文件夾。(根據(jù)引入類的多少自己定義)

將simple_html_dom.php復(fù)制到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','index\Spnovel@index');

三、創(chuàng)建控制器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{
	// 向服務(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ù)組和最后的效果。

laravel中如何使用simple_html_dom爬取顯示整本小說

五、運行

laravel中如何使用simple_html_dom爬取顯示整本小說

接下來就是每一章節(jié)的內(nèi)容了

先看路由

Route::get('/novel_con/{a}//{c}','index\Spnovel@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);
		$h2 = $html->find('.bookname h2');
		foreach ($h2 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é)的標(biāo)題和內(nèi)容,寫到數(shù)組中,并且去掉了多余的文字廣告部分。$next則是存放的下一章的地址,用于在章節(jié)詳情頁面跳轉(zhuǎn)。

視圖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>

解釋:因為只有當(dāng)前一篇所以不需要循環(huán),{{$artic['title']}}就是標(biāo)題,也可以寫到title中。{!!$artic['content']!!}的寫法就是不需要轉(zhuǎn)義文章的內(nèi)容,否則就會有很多其他字符了,如<br>等。下一章的按鈕的地址直接就用傳遞過來的即可,position:fixed固定定位按鈕,隨時可以下一章。

運行

laravel中如何使用simple_html_dom爬取顯示整本小說

以上是“l(fā)aravel中如何使用simple_html_dom爬取顯示整本小說”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI