├── .gitignore ├── README.md ├── fimfic.php ├── sina-article.php ├── sina-list.php ├── test ├── gethim.txt ├── ns.txt └── suntest.txt ├── tool-rev.php └── wenku8.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | *.mobi 3 | /test.php -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # advanced-php-crawler 2 | ## 新浪博客/FimFiction/wenku8轻小说文库 全能爬虫 3 | 4 | 这套PHP编写的小程序可以帮助喜欢在电子书阅读器上看新浪博客上文章的你,它可以根据已知的文章列表来爬行,亦可以根据已知的文章目录来爬行——你只需要将URL写入一个文本文件,接着调用程序即可。当然,你可以使用`#`符号作为注释,与bash类似。而且,它生成的是gitbook的标准格式,可以用gitbook/calibre工具自动生成多种格式(mobi/epub/pdf)的电子书。文章细节均已自动优化,爬行图片保存到本地,也生成封面,且使用MarkDown格式,只为带给你完美的阅读体验! 5 | ## 简明教程 6 | 淀粉月刊撰写的本程序简明教程:[https://dfkan.com/1635.html](https://dfkan.com/1635.html) 7 | ## 文件功能详解: 8 | 9 | 需要PHP5以上版本,Windows用户可安装[phpstudy](http://www.phpstudy.net/)。 10 | 11 | ### wenku8.php 12 | 13 | 它用于抓取wenku8.net(轻小说文库)的全本小说,生成分卷章节,打包mobi/epub电子书。 14 | 15 | > 输入wenku8.net的BookID,抓取并生成电子书。 16 | > 17 | > 使用方法: 18 | > 19 | > php wenku8.php 20 | > 21 | > 命令示例: 22 | > 23 | > php wenku8.php 1538 24 | > 25 | > #即为网址 https://www.wenku8.net/book/1538.htm 26 | 27 | ### sina-list.php 28 | 29 | 它用于爬行像这样子的博客文章目录: 30 | ![][image-1] 31 | 32 | > 新浪博客爬虫-列表爬虫 33 | > 34 | > -可以集合已知文章目录(/s/articlelist*)里面的文章列表 35 | > 36 | > 使用方法: 37 | > 38 | > php sina-list.php \<网址文件\> 39 | > 40 | > 参数解释: 41 | > 42 | > \<网址文件\>:一行一个网址,请使用电脑版访问后复制 43 | > 44 | > 命令示例: 45 | > 46 | > php sina-list.php urls.txt 47 | > 48 | > 网址文件示例: 49 | > 50 | > http://blog.sina.com.cn/s/articlelist_123456wsla.html #我是注释 51 | > 52 | > http://blog.sina.com.cn/s/articlelist_789456wsex.html 53 | > 54 | 55 | ### sina-article.php 56 | 它用于爬行像这样子的具体文章: 57 | ![][image-2] 58 | 59 | > 新浪博客爬虫-文章爬虫 60 | > 61 | > -可以提取已知文章页面(/s/blog*)里面的文章 62 | > 63 | > 使用方法: 64 | > 65 | > php sina-article.php \<网址文件\> 66 | > 67 | > 参数解释: 68 | > 69 | > \<网址文件\>:一行一个网址,请使用电脑版访问后复制 70 | > 71 | > 命令示例: 72 | > 73 | > php sina-article.php urls.txt 74 | > 75 | > 网址文件示例: 76 | > 77 | > http://blog.sina.com.cn/s/blog_123456wsla.html 78 | > 79 | > http://blog.sina.com.cn/s/blog_789456wsex.html 80 | > 81 | 82 | ### tool-rev.php 83 | 它用于把上面说的网址文件前前后后颠倒过来 84 | > 新浪博客爬虫-网址文件反转工具 85 | > 86 | > -将某个网址文件里面的url全部反转过来,可用于处理新旧文章顺序等 87 | > 88 | > 使用方法: 89 | > php tool-rev.php \<网址文件\> 90 | > 91 | > 参数解释: 92 | > 93 | > \<网址文件\>:一行一个网址,请使用电脑版访问后复制 94 | > 95 | > 命令示例: 96 | > 97 | > php tool-rev.php urls.txt 98 | > 99 | > 网址文件示例: 100 | > 101 | > http://blog.sina.com.cn/s/xxx_123456wsla.html 102 | > 103 | > http://blog.sina.com.cn/s/xxx_789456wsex.html 104 | > 105 | 106 | ### fimfic.php 107 | 这个脚本专门用于处理FimFiction的故事,同时包括抓取图片、调用彩云小译API翻译正文为中英双语对照格式。 108 | 109 | > 使用方法:php fimfic.php 110 | > 111 | > 命令示例:php fimfic.php https://www.fimfiction.net/story/318771/earth-without-us 112 | 113 | ## 附录 114 | [Gitbook安装全解](http://www.jianshu.com/p/7476afdd9248) 115 | 116 | [Gitbook+Calibre安装及使用](https://kindlefere.com/post/288.html#gb_6) 117 | 118 | [1]: https://kindlefere.com/post/82.html 119 | 120 | [image-1]: https://ww2.sinaimg.cn/large/006tNbRwgy1fdizlqnd8qj30s40i30wu.jpg 121 | [image-2]: https://ww1.sinaimg.cn/large/006tNbRwgy1fdizoxf1ivj30i40m7tgl.jpg -------------------------------------------------------------------------------- /fimfic.php: -------------------------------------------------------------------------------- 1 | 12 | 命令示例: 13 | php $argv[0] https://www.fimfiction.net/story/318771/earth-without-us 14 | ========== 15 | 16 | EOF; 17 | exit ; 18 | } 19 | $url = $argv[1]; 20 | if (!$url) { 21 | echo '网址为空 ' . $argv[1] . PHP_EOL; 22 | exit ; 23 | } 24 | $bookHtml=file_get_contents($url); 25 | preg_match('/\/story\/\d+/',$url,$m); 26 | $storyId=substr($m[0],7); 27 | preg_match('/\/story\/\d+\/(.*)/',$url,$m); 28 | $storyName=$m[1]; 29 | preg_match_all('/class="chapter-title" href="(.*?)"/',$bookHtml,$m); 30 | $chapterUrls=$m[1]; 31 | 32 | 33 | 34 | if(file_exists($storyName)){ 35 | delDirAndFile($storyName); 36 | echo '检测到此书本存在,已经删除旧的文件' . PHP_EOL; 37 | } 38 | mkdir($storyName); 39 | $img_name=$storyName.'/img'; 40 | mkdir($img_name); 41 | try{ 42 | preg_match('/property="og:image" content="(.*?)"/',$bookHtml,$m); 43 | $coverUrl=$m[1]; 44 | file_put_contents($storyName.'/cover.jpg',file_get_contents($coverUrl)); 45 | }catch(Exception $e){ 46 | echo '无法抓取封面' . PHP_EOL; 47 | } 48 | file_put_contents($storyName.'/SUMMARY.md','# 目录'.PHP_EOL.'* [说明](README.md)'. PHP_EOL); 49 | file_put_contents($storyName.'/README.md','# 说明'.PHP_EOL.'本电子书使用[FimFiction爬虫](https://github.com/juzeon/advanced-php-crawler)创建,祝你阅读愉快!'.PHP_EOL); 50 | echo PHP_EOL.PHP_EOL; 51 | foreach($chapterUrls as $count=>$url) { 52 | $url='https://www.fimfiction.net'.$url; 53 | echo '#现在处理:' . $url . PHP_EOL; 54 | for ($i = 0; $i < 5; $i++) { 55 | $result = workUrl($url); 56 | echo '#标题:' . $result['title'] . PHP_EOL; 57 | if (empty($result['content'])) { 58 | if($i==4){ 59 | echo '##ERROR:正则匹配失败,且超出5次重试次数,请打开网页检查问题。' . PHP_EOL . '---------' . PHP_EOL; 60 | break; 61 | } 62 | echo '##ERROR:正则匹配失败!重试次数'.($i+1) . PHP_EOL; 63 | sleep(2); 64 | continue; 65 | } else { 66 | echo '##内容有效' . PHP_EOL; 67 | } 68 | $content=$result['content']; 69 | preg_match_all('/class="user_image" src="(.*?)"/',$content,$img_matches); 70 | 71 | if(!empty($img_matches)){ 72 | mkdir($img_name.'/'.$count); 73 | for($j=0;$j'.PHP_EOL.PHP_EOL.'![img-'.$j.']('.'img/'.$count.'/'.$j.strrchr($img_matches[1][$j],'.').')'.PHP_EOL.PHP_EOL.'$storyName, 117 | 'description'=>'FimFiction爬虫于'.date('Y年m月d日 H:i').'生成的电子书', 118 | 'language'=>'en-us', 119 | 'author'=>'FimFiction爬虫' 120 | ); 121 | $book_json=json_encode($json_book); 122 | file_put_contents($storyName.'/book.json',$book_json); 123 | echo '#全部任务处理完毕' . PHP_EOL . '---------' . PHP_EOL; 124 | echo '#你可以编辑book.json来修改这本书的详细信息,然后——'.PHP_EOL; 125 | echo '#使用gitbook工具输出为mobi格式电子书:gitbook mobi '.$storyName.'/ '.$storyName.'.mobi'.PHP_EOL; 126 | echo '#使用gitbook工具输出为epub格式电子书:gitbook epub '.$storyName.'/ '.$storyName.'.epub'.PHP_EOL; 127 | echo '#注意:需要安装gitbook和calibre/ebook-convert,参考资料:'.PHP_EOL; 128 | echo 'http://www.jianshu.com/p/7476afdd9248'.PHP_EOL; 129 | echo 'https://kindlefere.com/post/288.html#gb_6'.PHP_EOL; 130 | echo '===========' . PHP_EOL . PHP_EOL; 131 | 132 | 133 | function delDirAndFile( $dirName ) 134 | { 135 | if ( $handle = opendir( "$dirName" ) ) { 136 | while ( false !== ( $item = readdir( $handle ) ) ) { 137 | if ( $item != "." && $item != ".." ) { 138 | if ( is_dir( "$dirName/$item" ) ) { 139 | delDirAndFile( "$dirName/$item" ); 140 | } else { 141 | unlink( "$dirName/$item" ); 142 | } 143 | } 144 | } 145 | closedir( $handle ); 146 | rmdir( $dirName ); 147 | } 148 | } 149 | function workUrl($url) { 150 | $content=''; 151 | $article=file_get_contents(trim($url)); 152 | preg_match('/(
)([\s\S]*?)