下面是使用httpclient爬蟲爬取某個網站的漢字相關信息的實踐代碼,中間遇到了一些字符格式的問題。以前被同事見過用html解析類來抓取頁面信息,而不是像我如今用正則,常常嘗試,效果並很差,畢竟頁面放爬蟲仍是很是好作的。在本次實踐中,就遇到了相關的難點,因此仍是纔去了正則提取的方式。分享代碼,供你們參考。關鍵信息並未隱去。html
public static void main(String[] args) throws SQLException { DEFAULT_CHARSET = GB2312; List<String> list = WriteRead.readTxtFileByLine(LONG_Path + "word.log"); list.forEach(py -> { getPYAndWord(py); }); testOver(); } public static void getPYAndWord(String py) { output(py); String url = "http://zd.diyifanwen.com/zidian/py/" + py + ".htm"; HttpGet httpGet = getHttpGet(url); JSONObject response = getHttpResponse(httpGet); // output(response); String content = response.getString("content"); String all = new String(content.getBytes(UTF_8), UTF_8); List<String> regexAll = new ArrayList<>(); List<String> alllist = regexAll(all, "http://zd.d.*?>[\\u4e00-\\u9FFF]<"); output(alllist.size()); alllist.forEach(line -> { String murl = regexAll(line, "http://zd.diyifanwen.com/zidian/\\w/\\d+.htm").get(0); String mword = regexAll(line, ">[\\u4e00-\\u9fa5]<").get(0); regexAll.add(mword); output(murl, mword); String sql = "INSERT INTO chinese_dictionary_word (word,url) VALUES (\"%s\",\"%s\");"; sql = String.format(sql, mword.replaceAll("<|>", EMPTY), murl); output(sql); MySqlTest.sendWork(sql); }); String str = regexAll.toString().replaceAll("<|>|\\[|\\]", EMPTY); String sql = "INSERT INTO chinese_dictionary_py_word (py,words) VALUES (\"%s\",\"%s\");"; sql = String.format(sql, py, str); output(sql); MySqlTest.sendWork(sql); sleep(2); } /**獲取拼音列表 * @return */ public static String getPY() { String url = "http://zd.diyifanwen.com/zidian/py/"; HttpGet httpGet = getHttpGet(url); JSONObject response = getHttpResponse(httpGet); // output(response); String content = response.getString("content"); byte[] bytes = content.getBytes(UTF_8); String all = new String(bytes, UTF_8); Log.log("content", all); return all; } /**獲取全部首字母和拼音 * @param all */ public static void getAllPY(String all) { List<String> list = regexAll(all, "<dt class=\"pyTitle\">拼音首字母\\w+</dt>" + LINE + ".+/dd>"); list.forEach(s -> { int num = s.indexOf("拼音首字母"); String first = s.substring(num + 5, num + 6); List<String> list1 = regexAll(s, "http://zd.diyifanwen.com/zidian/py/\\w+.htm"); list1.forEach(str -> { int one = str.indexOf("/py/"); int two = str.lastIndexOf("."); String second = str.substring(one + 4, two); String sql = "INSERT INTO chinese_dictionary_py (first_word,all_word) VALUES (\"%s\",\"%s\");"; String sqlEnd = String.format(sql, first, second); MySqlTest.sendWork(sqlEnd); }); }); } /**檢查拼音是否所有獲取到 * @param all */ public static void checkPY(String all) { List<String> list = regexAll(all, "zidian/py/\\w+.htm"); list.forEach(str -> { int one = str.indexOf("/py/"); int two = str.lastIndexOf("."); String second = str.substring(one + 4, two); output(second); String sql = "SELECT * FROM chinese_dictionary_py WHERE all_word = \"%s\";"; String sq = String.format(sql, second); ResultSet resultSet = MySqlTest.excuteQuerySql(sq); try { if (!resultSet.next()) output(sq); } catch (SQLException e) { e.printStackTrace(); } }); } /**從數據庫中查找當前獲取的拼音並存儲到文件中 * @throws SQLException */ public static void getAllPY() throws SQLException { List<String> word = new ArrayList<>(); ResultSet resultSet = MySqlTest.excuteQuerySql("SELECT all_word FROM chinese_dictionary_py;"); while (resultSet.next()) { String string = resultSet.getString(1); word.add(string); } Save.saveStringList(word, "word"); }
結果如圖:java
對於漢字具體的釋義內容並未爬取,鏈接進行了保存。sql