前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >记一次失败的爬虫

记一次失败的爬虫

作者头像
FunTester
发布2020-02-17 16:15:15
3080
发布2020-02-17 16:15:15
举报
文章被收录于专栏:FunTesterFunTester

收到一天振奋人心的假新闻,导致我去找了公开信息网站定点药店的信息,虽然结果比较失败,过程还是挺欢乐的,记录下来又可以水一篇文章了。以下是原文:


页面搜索功能有限,我就做了一个爬虫,思路分了两步,先找药店名称的编号,再查药店的具体地址。

这里面只有773条网点信息,我顺手做了个爬虫,代码粗糙,十几分钟完成的,效果还可以。代码分享如下:

代码语言:javascript
复制

package com.fun

import com.fun.frame.Save
import com.fun.frame.httpclient.FanLibrary
import com.fun.utils.Regex
import com.fun.utils.WriteRead
import net.sf.json.JSONObject
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost

class sd extends FanLibrary {

    static names = []

    public static void main(String[] args) {

        test2(1)
//        52.times {
//            test(it+1)
//        }
//        Save.saveStringList(names,"hospital")

        def line = WriteRead.readTxtFileByLine(LONG_Path + "hospital")
        line.each {
            def s = it.split(" ", 3)[1]
            output(s)
            it += test2(changeStringToInt(s))
            names << it
        }
        Save.saveStringList(names,"address.txt")

        testOver()
    }

    static def test(int i) {
        String url = "http://ybj.***.gov.cn/ddyy/ddyy2/list";

        HttpPost httpPost = getHttpPost(url, getJson("page=" + i));

        JSONObject response = getHttpResponse(httpPost);
        def all = response.getString("content").replaceAll("\\s", EMPTY)

        def infos = Regex.regexAll(all, "<tr>.*?</tr>")
        infos.remove(0)
        output(infos)
        output(infos.size())
        infos.each {x ->
            names << x.substring(4).replaceAll("<.*?>", SPACE_1)
        }
        output(names.size())
    }
    static def test2(int i) {
        String url = "http://ybj.***.gov.cn/ddyy/ddyy2/findByName?id="+i;

        HttpGet httpGet = getHttpGet(url);

        JSONObject response = getHttpResponse(httpGet);
        def all = response.getString("content").replaceAll("\\s", EMPTY)

        def infos = Regex.regexAll(all, "<tr>.*?</tr>")
//        infos.remove(0)
        output(infos)
//        output(infos.size())
        def address = EMPTY
        infos.each {x ->
//            names << x.substring(4).replaceAll("<.*?>", SPACE_1)
           address += x.substring(4).replaceAll("<.*?>", SPACE_1)
        }
        output(address)
        return address
    }
}

下面是两次爬虫的页面结构:

篇幅有限,我已经整理了Excel文档。需要的小伙伴添加微信索要。


  • 郑重声明:文章首发于公众号“FunTester”,禁止第三方(腾讯云除外)转载、发表。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FunTester 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档