「All right reserved, any unauthorized reproduction or transfer is prohibitted」
        
      近期搜电子是的时候发现一个有趣的网站,很多精校版的电子书,由于好奇,就想做一个爬虫把名称汇总一下。(具体原因在于 canvas 的页面背景效果在 Chrome 浏览器里面特别消耗资源)自己去搜索书名,然后找下载地址。十几分钟,脚本基本写完,一晚上时间也差不多能够跑完了。
分享代码,仅供参考(比较粗糙)。
package com.fun
import com.fun.db.mysql.MySqlTest
import com.fun.frame.httpclient.FanLibrary
import com.fun.utils.Regex
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class T extends FanLibrary {
    static Logger logger = LoggerFactory.getLogger(T.class)
    public static void main(String[] args) {
//        test(322)
        def list = 1..1000 as List
        list.each { x ->  
            try {
                test(x)
            } catch (Exception e) {∫
                logger.error(x.toString())
                output(e)
            }
            logger.warn(x.toString())
            sleep(2000)
        }
        testOver()
    }
    static def test(int id) {
//        def get = getHttpGet("https://****/books/9798.html")
        def get = getHttpGet("https://****/books/" + id + ".html")
        def response = getHttpResponse(get)
        def string = response.getString("content")
        if (string.contains("您需求的文件不存在")|| string.contains("页面未找到")) return
        output(string)
        def all = Regex.regexAll(string, "class=\"bookpic\"> <img title=\".*?\"").get(0)
        def all2 = Regex.regexAll(string, "content=\"内容简介.*?\"").get(0)
        def all3 = Regex.regexAll(string, "title=\"作者:.*?\"").get(0)
        def all40 = Regex.regexAll(string, "https://sobooks\\.cc/go\\.html\\?url=https{0,1}://.*?\\.ctfile\\.com/.*?\"")
        def all4 = all40.size() == 0 ? "" : all40.get(0)
        def all50 = Regex.regexAll(string, "https://sobooks\\.cc/go\\.html\\?url=https{0,1}://pan\\.baidu\\.com/.*?\"")
        def all5 = all50.size() == 0 ? "" : all50.get(0)
        output(all)
        output(all2)
        output(all3)
        output(all4)
        output(all5)
        def name = all.substring(all.lastIndexOf("=") + 2, all.length() - 1)
        def author = all3.substring(all3.lastIndexOf("=") + 2, all3.length() - 1)
        def intro = all2.substring(all2.lastIndexOf("=") + 2, all2.length() - 1)
        def url1 = all4 == "" ? "" : all4.substring(all4.lastIndexOf("=") + 1, all4.length() - 1)
        def url2 = all5 == "" ? "" : all5.substring(all5.lastIndexOf("=") + 1, all5.length() - 1)
        output(name, author, intro, url1, url2)
        def sql = String.format("INSERT INTO books (name,author,intro,urlc,urlb,bookid) VALUES (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%d)", name, author, intro, url1, url2, id)
        MySqlTest.sendWork(sql)
    }
}
 
个人感觉还是比较满意的。

公众号后台回复 “电子书” 可得网站地址和 CSV 文件下载地址。
技术类文章精选
- java 一行代码打印心形
 - Linux 性能监控软件 netdata 中文汉化版
 - 接口测试代码覆盖率(jacoco)方案分享
 - 性能测试框架
 - 如何在 Linux 命令行界面愉快进行性能测试
 - 图解 HTTP 脑图
 - 如何测试概率型业务接口
 - httpclient 处理多用户同时在线
 - 将 swagger 文档自动变成测试代码
 - 五行代码构建静态博客
 - httpclient 如何处理 302 重定向
 - 基于 java 的直线型接口测试框架初探
 - Tcloud 云测平台 -- 集大成者
 - 如何测试概率型业务接口
 - python plotly 处理接口性能测试数据方法封装
 - 单点登录性能测试方案
 
非技术文章精选
- 为什么选择软件测试作为职业道路?
 - 成为杰出 Java 开发人员的 10 个步骤
 - 写给所有人的编程思维
 - 自动化测试的障碍
 - 自动化测试的问题所在
 - 测试之《代码不朽》脑图
 - 成为优秀自动化测试工程师的 7 个步骤
 - 优秀软件开发人员的态度
 - 如何正确执行功能 API 测试
 - 未来 10 年软件测试的新趋势 - 上
 - 未来 10 年软件测试的新趋势 - 上
 - 自动化测试解决了什么问题
 - 17 种软件测试人员常用的高效技能 - 上
 - 17 种软件测试人员常用的高效技能 - 下
 - 手动测试存在的重要原因
 - 编写测试用例的 17 个技巧
 
大咖风采

          TesterHome 为用户提供「保留所有权利,禁止转载」的选项。
          除非获得原作者的单独授权,任何第三方不得转载标注了「All right reserved, any unauthorized reproduction or transfer is prohibitted」的内容,否则均视为侵权。
          具体请参见TesterHome 知识产权保护协议。
  
    如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!