专栏文章 微信公众号文章爬虫实践

FunTester · 2022年02月08日 · 1105 次阅读

年前发了一篇 FunTester 公众号原创文章总结FunTester 原创大赏,但是整理的时候却发现自己没有记录文章的发表日期,导致有一些文章由于发表日志过早(且排名靠前)影响了一丝阅读体验,所以我想了一个办法爬取了每篇文章的发表时间,在自己整理的 Markdown 文档中增加发表日期内容。

经过简单验证,决定使用接口爬虫功能来实现这个需求。

日期获取

经过页面的检查,发现的确存在发表日期的记录数据,隐藏在巨大的信息当中,不过有意思的是,微信公众号的公共访问内容居然全文只有一处日期且为真正的发表日期,所以也大大节省了我的时间。

下面是我的封装方法

static def test(String url) {
    //        def url = "https://mp.weixin.qq.com/s/1UFooul_azwH8k0dylDv3A"
    def key = url.substring(url.lastIndexOf("/") + 1)
    def get = getHttpGet(url)
    def response = getHttpResponse(get)
    def res = response.getString("content")
    //        output(string)
    //        output(res)
    def all = Regex.regexAll(res, "20[1,2]\\d\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}")
    def s = all[0]
    output(key + PART + s)
}

原创文章链接

下面分享一下我原来的记录 Markdown 文档的内容截取:


- [分布式性能测试框架用例方案设想(二)](https://mp.weixin.qq.com/s/bs65VVvRoB0AGyyfwSgT0w)
- [基于docker的分布式性能测试框架功能验证(二)](https://mp.weixin.qq.com/s/49fAcamsteo7yVjO8JmQrg)
- [分布式性能测试框架单节点内测](https://mp.weixin.qq.com/s/IwEQGC2rOgcT7deCWW4wDw)
- [分段随机实践—模拟线上流量](https://mp.weixin.qq.com/s/qUPhtG5lZIWx6oYrgwEuag)
- [性能测试框架对比初探](https://mp.weixin.qq.com/s/w46QciCvh6dPswBA42oiIQ)
- [性能框架哪家强—JMeter、K6、locust、FunTester横向对比](https://mp.weixin.qq.com/s/BGs3ImdkRGFB-h3fxUktmw)
- [分布式性能测试框架用例方案设想(三)](https://mp.weixin.qq.com/s/hkSn4g9Z3sfWIV9-dOiS7Q)
- [基于docker的分布式性能测试框架功能验证(三)](https://mp.weixin.qq.com/s/mBEuEWlrw_gwV0T6S93LKA)
- [10万QPS,K6、Gatling和FunTester终极对决!](https://mp.weixin.qq.com/s/HZvBPUEaws72hlwb1QXzuw)
- [单机12万QPS——FunTester复仇记](https://mp.weixin.qq.com/s/4IBaEpj3TEHY_2ZdGOON0g)
- [分布式请求放大器实现](https://mp.weixin.qq.com/s/JAzVyP2u9WicNXvVamBGYQ)
- [FunTester框架Redis性能测试之list操作](https://mp.weixin.qq.com/s/WBjPdpc4RNZ-rUXhS9qdSg)
- [全链路压测流量模型](https://mp.weixin.qq.com/s/nSJKvRgrh87LJjDQRBUtdw)
- [FunTester框架Redis性能测试之map&INCR](https://mp.weixin.qq.com/s/SJtD4mxOUCSfcSTfhbA5Jw)

思路就是分行去读,然后获取每一行的 URL 链接,然后调用爬虫获取日期,然后我先存在了本地,并没有使用 LevelDB,原因是因为爬虫都是一次性的,没必要存在本地的 LevelDB 里面。其实保存的方式也在上面爬虫的方法中,就是通过日志输出,避免爬虫中断。

PS:这里休眠了 3 秒,避免触发反爬虫规则,自测是可行的。

static def spider() {
    String path = "/Users/oker/IdeaProjects/funtester/document/directory.markdown"
    def line = RWUtil.readByLine(path)
    //        output(line)
    def key = false
    line.each {
        if (key && it.startsWith("- [") && it.contains("weixin.qq")) {
            String url = it.substring(it.lastIndexOf("]") + 2) - ")"
            //                output(url)
            test(url)
            //                fail()
            sleep(3.0)
        }
    }

}

重写 Markdown

我将爬取到的数据存在文件中,然后再读取到一个com.alibaba.fastjson.JSONObject中。再重新读取原来的 Markdown 文件,截取 URL 最后的一段(此段 String 也是 JSONObject 数据中的 key),从 JSONObject 中去读到日期,然后拼接文案。

public static void main(String[] args) {
    def string = RWUtil.readByString(getLongFile("wx"))
    def info = parse(string)
    String path = "/Users/oker/IdeaProjects/funtester/document/directory.markdown"
    def line = RWUtil.readByLine(path)
    line.each {
        if (it.startsWith("- [") && it.contains("weixin.qq")) {
            String url = it.substring(it.lastIndexOf("]") + 2) - ")"
            def key = url.substring(url.lastIndexOf("/") + 1)
            output("$it $TAB 发表于${info.get(key)}")
        } else {
            output(LINE + it + LINE)
        }
    }


}

实际效果如何大家可以点击阅读原文查看。

Have Fun ~ Tester !

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册