PHP實現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能示例
本文實例講述了PHP實現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能。分享給大家供大家參考,具體如下:
爬取免費內(nèi)容,弄到手機,聽書,妥妥的。
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');
ini_set('max_execution_time', '0');
$base = 'https://www.qu.la/book/19434/';
$start = '7504808.html';
$content_grep = '/ (.*)<br\/>/';
//$content_grep = '/<div id="content">(.*)<br\/>/sS';
$next_grep = '/<a id="pager_next" href=\"(\d+\.html)\" target="_top" class="next">下一章<\/a>/';
$next = $start;
$file_name = '聽書了.txt';
while($next) {
echo 'getting ' . $next . PHP_EOL;
$result = file_get_contents($base . $next);
preg_match_all($content_grep, $result, $match);
$isTitle = true;
$content = "";
foreach($match[1] as $line) {
$line = str_replace("<br/>", '', $line);
$line = str_replace(" ", '', $line);
if($isTitle) {
$content = $line . PHP_EOL . PHP_EOL;
$isTitle = false;
} else {
$content .= ' ' . $line . PHP_EOL . PHP_EOL;
}
}
$file = fopen($file_name, 'a');
echo 'write length: ' . strlen($content) . PHP_EOL;
fwrite($file, $content);
fclose($file);
echo '.';
preg_match($next_grep, $result, $match);
$next = $match[1];
}
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php socket用法總結》、《php字符串(string)用法總結》、《PHP數(shù)學運算技巧總結》、《php面向對象程序設計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》及《PHP網(wǎng)絡編程技巧總結》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP基于rabbitmq操作類的生產(chǎn)者和消費者功能示例
這篇文章主要介紹了PHP基于rabbitmq操作類的生產(chǎn)者和消費者功能,結合實例形式分析了基于rabbitmq操作類的生產(chǎn)者和消費者定義與使用方法,需要的朋友可以參考下2018-06-06
PHP中unset,array_splice刪除數(shù)組中元素的區(qū)別
php中刪除數(shù)組元素是非常的簡單的,但有時刪除數(shù)組需要對索引進行一些排序要求我們會使用到相關的函數(shù),這里我們來介紹使用unset,array_splice刪除數(shù)組中的元素區(qū)別吧2014-07-07
php中simplexml_load_file函數(shù)用法實例
這篇文章主要介紹了php中simplexml_load_file函數(shù)用法,以實例形式詳細的講述了simplexml_load_file函數(shù)讀取XML文件的具體方法,非常具有實用價值,需要的朋友可以參考下2014-11-11
PHP實現(xiàn)基于圖的深度優(yōu)先遍歷輸出1,2,3...n的全排列功能
這篇文章主要介紹了PHP實現(xiàn)基于圖的深度優(yōu)先遍歷輸出1,2,3...n的全排列功能,涉及php數(shù)據(jù)結構中圖的遍歷及全排列相關數(shù)學運算技巧,需要的朋友可以參考下2017-11-11
Ubuntu server 11.04安裝memcache及php使用memcache來存儲session的方法
這篇文章主要介紹了Ubuntu server 11.04安裝memcache及php使用memcache來存儲session的方法,涉及memcache服務器的安裝及php操作memcache存儲session的相關技巧,需要的朋友可以參考下2016-05-05

