从国家统计局爬取山东省市区县乡镇
来源:互联网 发布:注销淘宝店铺重新开通 编辑:程序博客网 时间:2024/04/29 20:51
项目需要,写了个简单的程序爬去山东省市区县乡镇区划信息。
依赖的jar包来源于httpcomponents-client-4.2.5-bin.zip;
package org.apache.http.examples.test;import java.io.BufferedReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;//从国家统计局爬取山东省市区县乡镇public class DailySign { public static final String URL_GET = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2014/"; public static int count = 0; public static String pre="12"; public static String file = pre+".html"; public static String fileName = pre+".html"; public static FileWriter writer = null; public static StringBuffer sb = new StringBuffer(); public static Pattern p = Pattern.compile("<a href='([^>]*)'>([^<]*)</a>"); public static void main(String[] args) throws Exception { writer = new FileWriter(fileName+".txt"); get("",file); try { writer.write(sb.toString()); if(writer!=null){ writer.close(); }} catch (IOException e) {e.printStackTrace();} } public static void get(String prefix, String req) throws ClientProtocolException, IOException, InterruptedException { count++; //休眠,防止大量请求被网站拒绝 if(count % 200 == 0){ Thread.sleep(1000); } DefaultHttpClient client = new DefaultHttpClient(); HttpGet signGet = new HttpGet(URL_GET + prefix + req); // 执行签到请求 HttpResponse signResponse = client.execute(signGet); // 处理响应 showResult(signResponse); } /** * 读取相应内容并输出 * @throws InterruptedException */ public static void showResult(HttpResponse response) throws IOException, UnsupportedEncodingException, InterruptedException { int status = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); InputStream instream = null; if (entity != null) { instream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(instream,"GBK")); String line = null; while ((line = reader.readLine()) != null) { line = new String(line.getBytes(), "UTF-8"); if (line.startsWith("<tr class='citytr'>") || line.startsWith("<tr class='countytr'>") || line.startsWith("<tr class='towntr'>")) { Matcher m = p.matcher(line); while (m.find()) { String code = m.group(1); String name = m.group(2); if (name.startsWith(pre)) { System.out.print(name + "\t"); sb.append(name + "\t"); } else { System.out.print(code + "\t"); sb.append(code + "\t"); sb.append(name+"\r\n"); System.out.println(name); String prefix = ""; if (line.startsWith("<tr class='countytr'>")) { prefix = "/"+code.substring(3, 5); } //递归 get(prefix, "/"+code); } } } } instream.close(); EntityUtils.consume(entity); } }}
0 0
- 从国家统计局爬取山东省市区县乡镇
- sql 省市区县
- 省市区县sql语句
- 全国省市区县 MySQL 数据库 含行政区划编码 名称 父级行政区划编码 基于国家统计局2017年3月发布数据
- 全国省市区县数据库脚本
- 中国省市区县最新数据表
- 全国省市区县数据库脚本
- 中国 省市区县 数据库sql
- 中国 省市区县 数据库sql
- 中国 省市区县 数据库sql
- 全国省市区县数据库(全)
- 中国 省市区县 数据库SQL 脚本
- 获取高德地图省市区县列表
- 省市区县三级联动JAVA+MySQL+JQuery
- 获取高德地图省市区县列表
- 省市区县三级联动JAVA+MySQL+JQuery
- 关于echart3地图下钻省市区县
- 百度地图 省市区县 信息展示
- Java Reflection(六):Getters and Setters
- confluence创建文件报错 can not create to file /tmp/xxx Errcode:28,引出的 df命令高级用法
- Ubuntu14下OpenCV3和QT5的安装及联合使用
- Android.mk详解
- JavaScript prototype 属性
- 从国家统计局爬取山东省市区县乡镇
- java 读取文件的最后若干行
- 关于安卓开发webview与js交互的问题
- 关于一段js中出现多次使用setInterval(出现计时累加)的问题
- android 指纹验证api
- java中HashSet浅析
- behemoth - 07
- 设计模式之命令模式
- 【CSS3】:read-write选择器