PHP实现的抓取小说网站内容功能示例

2025-05-29 0 39

本文实例讲述了PHP实现的抓取小说网站内容功能。分享给大家供大家参考,具体如下:

爬取免费内容,弄到手机,听书,妥妥的。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33
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 = '/&nbsp;&nbsp;&nbsp;&nbsp;(.*)<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程序设计有所帮助。

原文链接:https://blog.csdn.net/Alen_xiaoxin/article/details/79567579

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现的抓取小说网站内容功能示例 https://www.kuaiidc.com/92237.html

相关文章

发表评论
暂无评论