前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >接口爬虫之网页表单数据提取

接口爬虫之网页表单数据提取

作者头像
FunTester
发布2019-09-17 15:31:15
8020
发布2019-09-17 15:31:15
举报
文章被收录于专栏:FunTesterFunTester

本人最近接到一项任务,要爬一项数据,这个数据在某个网页的表格里面,数据量几百。打开调试模式发现接口返回的就是一个html页面,只要当做string处理。(解析html文件用xpath爬虫有些麻烦)方案采用了正则匹配所有的单元行,然后提取单元格内容,这里面遇到了一些其他问题:

  1. 本来采用直接提取内容,发现内容涉及各国语言文字,有点坑,不搞了。
  2. 截取完单元行之后,发现两个字段内容之间有空格,且数量不确定,使用了spit方法限制数组大小
  3. 编码格式不正确导致乱码

分享代码供大家参考:

代码语言:javascript
复制
public static void main(String[] args) {
        String url = "https://docs.oracle.com/cd/E13214_01/wli/docs92/xref/xqisocodes.html";
        HttpGet httpGet = getHttpGet(url);
        JSONObject httpResponse = getHttpResponse(httpGet);
        String content = httpResponse.getString("content");
        List<String> strings = regexAll(content, "<tr.+</a>" + LINE + ".+" + LINE + ".+" + LINE + ".+" + LINE + ".+" + LINE + ".+" + LINE + "</div>");
        int size = strings.size();
        for (int i = 0; i < size; i++) {
            String s = strings.get(i).replaceAll("<.+>", EMPTY).replaceAll(LINE, EMPTY);
            String[] split = s.split(" ", 2);
            String sql = "INSERT country_code (country,code) VALUES (\"%s\",\"%s\");";
            output(String.format(sql, split[0].replace(SPACE_1, EMPTY), split[1].replace(SPACE_1, EMPTY)));
        }
        testOver();
    }

其中的一些封装方法如下:

代码语言:javascript
复制
/**
     * 返回所有匹配项
     *
     * @param text  需要匹配的文本
     * @param regex 正则表达式
     * @return
     */
    public static List<String> regexAll(String text, String regex) {
        List<String> result = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            result.add(matcher.group());
        }
        return result;
    }

最终拼接的sql部分结果为:

代码语言:javascript
复制
INSERT country_code (country,code) VALUES ("German","de");
INSERT country_code (country,code) VALUES ("Greek","el");
INSERT country_code (country,code) VALUES ("Greenlandic","kl");
INSERT country_code (country,code) VALUES ("Guarani","gn");
INSERT country_code (country,code) VALUES ("Gujarati","gu");
INSERT country_code (country,code) VALUES ("Hausa","ha");
INSERT country_code (country,code) VALUES ("Hebrew","he");
INSERT country_code (country,code) VALUES ("Hindi","hi");
INSERT country_code (country,code) VALUES ("Hungarian","hu");
INSERT country_code (country,code) VALUES ("Icelandic","is");
INSERT country_code (country,code) VALUES ("Indonesian","id");
INSERT country_code (country,code) VALUES ("Interlingua","ia");
INSERT country_code (country,code) VALUES ("Interlingue","ie");
INSERT country_code (country,code) VALUES ("Inuktitut","iu");
INSERT country_code (country,code) VALUES ("Inupiak","ik");
INSERT country_code (country,code) VALUES ("Irish","ga");
INSERT country_code (country,code) VALUES ("Italian","it");
INSERT country_code (country,code) VALUES ("Japanese","ja");

技术类文章精选

  1. java一行代码打印心形
  2. Linux性能监控软件netdata中文汉化版
  3. 接口测试代码覆盖率(jacoco)方案分享
  4. 性能测试框架
  5. 如何在Linux命令行界面愉快进行性能测试
  6. 图解HTTP脑图
  7. 如何测试概率型业务接口
  8. httpclient处理多用户同时在线
  9. 将swagger文档自动变成测试代码
  10. 五行代码构建静态博客
  11. httpclient如何处理302重定向
  12. 基于java的直线型接口测试框架初探

非技术文章精选

  1. 为什么选择软件测试作为职业道路?
  2. 成为杰出Java开发人员的10个步骤
  3. 写给所有人的编程思维
  4. 自动化测试的障碍
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 技术类文章精选
  • 非技术文章精选
相关产品与服务
应用性能监控
应用性能监控(Application Performance Management,APM)是一款应用性能管理平台,基于实时多语言应用探针全量采集技术,为您提供分布式性能分析和故障自检能力。APM 协助您在复杂的业务系统里快速定位性能问题,降低 MTTR(平均故障恢复时间),实时了解并追踪应用性能,提升用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档