介面爬蟲之網頁表單數據提取

  • 2019 年 10 月 4 日
  • 筆記

本人最近接到一項任務,要爬一項數據,這個數據在某個網頁的表格裡面,數據量幾百。打開調試模式發現介面返回的就是一個html頁面,只要當做string處理。(解析html文件用xpath爬蟲有些麻煩)方案採用了正則匹配所有的單元行,然後提取單元格內容,這裡面遇到了一些其他問題:

  1. 本來採用直接提取內容,發現內容涉及各國語言文字,有點坑,不搞了。
  2. 截取完單元行之後,發現兩個欄位內容之間有空格,且數量不確定,使用了spit方法限制數組大小
  3. 編碼格式不正確導致亂碼

分享程式碼供大家參考:

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();      }  

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

/**       * 返回所有匹配項       *       * @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部分結果為:

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. 自動化測試的障礙