package com.xazhxc.htjcom.back.controller.base;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xazhxc.htjcom.entity.Citys;
import com.xazhxc.htjcom.init.HttpServerInit;
import com.xazhxc.htjcom.kit.Kits;
import com.xazhxc.htjcom.kit.PropsKit;
import com.xazhxc.htjcom.kit.UploadKit;
import com.xazhxc.htjcom.service.CitysService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.tio.core.ChannelContext;
import org.tio.core.GroupContext;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.common.UploadFile;
import org.tio.http.server.annotation.RequestPath;
import org.tio.http.server.mvc.Routes;
import org.tio.http.server.util.Resps; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* 公共类
*
* @author leizhen.wang
*/
@RequestPath(value = "/base")
@Slf4j
public class BaseController {
static CitysService citysService = Kits.getBean( CitysService.class);
@Mapper
ProductService productService; private static Map<Integer, String> cssMap = new HashMap<Integer, String>();
private static BufferedWriter bufferedWriter = null;
static {
cssMap.put(1, "provincetr");// 省
cssMap.put(2, "citytr");// 市
cssMap.put(3, "countytr");// 市
}
@RequestPath(value = "/pro")
public HttpResponse product(HttpRequest request) throws IOException { new Thread(() -> {
try {
initFile();
Document connect = Jsoup.connect( "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/" ).get();
int level = 1;
Elements rowProvince = connect.select("tr." + cssMap.get(level));
for (Element provinceElement : rowProvince) {
Elements select = provinceElement.select("a");
for (Element province : select) {
try {
parseNextLevel(province, level + 1, null, null);
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println("----province-----"+province);
}
}
closeStream();
} catch (IOException e) {
e.printStackTrace();
}
}).start(); return Resps.json( request, Kits.result().ok() );
} private static void closeStream() {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} private static void parseNextLevel(Element parentElement, int level, String code, String area) throws IOException {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} String attr = parentElement.attr( "abs:href" );
if (StrUtil.isEmpty( code )) {
code = attr.substring( 54,56 )+"0000";
}
// Citys citys = new Citys(String.valueOf( SnowFlakeUtil.getFlowIdInstance().nextId() ), code, parentElement.text(), "-1");
// citysService.insert( citys );
Document doc = Jsoup.connect( attr).get();
if (doc != null) {
Elements newsHeadlines = doc.select("tr." + cssMap.get(level));
for (Element element : newsHeadlines) {
if (StrUtil.isEmpty( area )) {
// printInfo(element, level + 1, code);
} else {
printInfo2(element, level + 1, code);
}
Elements select = element.select("a");// 在递归调用的时候,这里是判断是否是村一级的数据,村一级的数据没有a标签
// System.out.println(select);
if (select.size() != 0) {
code = element.select("td").first().text();
parseNextLevel2(select.last(), level + 1, code, "area");
}
}
} } private static void parseNextLevel2(Element parentElement, int level, String code, String area) throws IOException {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
String attr = parentElement.attr( "abs:href" );
if (StrUtil.isEmpty( code )) {
code = attr.substring( 54,56 )+"0000";
}
Document doc = Jsoup.connect( attr).get();
if (doc != null) {
Elements newsHeadlines = doc.select("tr." + cssMap.get(level));
for (Element element : newsHeadlines) {
printInfo2(element, level + 1, code);
}
} } private static void initFile() throws IOException {
bufferedWriter = new BufferedWriter(new FileWriter(new File("d:\\CityInfo.txt"), true));
}
private static void printInfo(Element element, int level, String pid) {
// System.out.println(pid + "---: "+element.select("td").last().text()+"============="+element.select("td").first().text());
Citys citys = new Citys(String.valueOf( SnowFlakeUtil.getFlowIdInstance().nextId() ), element.select("td").first().text(), element.select("td").last().text(), pid);
citysService.insert( citys );
/*try {
bufferedWriter.write(element.select("td").last().text() + "{" + level + "}["
+ element.select("td").first().text() + "]");
bufferedWriter.newLine();
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}*/
} private static void printInfo2(Element element, int level, String pid) {
// System.out.println(pid + "---: "+element.select("td").last().text()+"============="+element.select("td").first().text());
Citys citys = new Citys(String.valueOf( SnowFlakeUtil.getFlowIdInstance().nextId() ), element.select("td").first().text(), element.select("td").last().text(), pid);
citysService.insert( citys );
/*try {
bufferedWriter.write(element.select("td").last().text() + "{" + level + "}["
+ element.select("td").first().text() + "]");
bufferedWriter.newLine();
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}*/
} }

  

jsoup 抓取省市区的更多相关文章

  1. jsoup抓取网页内容

    java项目有时候我们需要别人网页上的数据,怎么办?我们可以借助第三方架包jsou来实现,jsoup的中文文档,那怎么具体的实现呢?那就跟我一步一步来吧 最先肯定是要准备好这个第三方架包啦,下载地址, ...

  2. jsoup抓取网页+具体解说

    jsoup抓取网页+具体解说 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目.我以前在 IBM DW 上发表过两篇关于 htmlparser 的文章.各自 ...

  3. Jsoup抓取网页数据完成一个简易的Android新闻APP

    前言:作为一个篮球迷,每天必刷NBA新闻.用了那么多新闻APP,就想自己能不能也做个简易的新闻APP.于是便使用Jsoup抓取了虎扑NBA新闻的数据,完成了一个简易的新闻APP.虽然没什么技术含量,但 ...

  4. 使用java开源工具httpClient及jsoup抓取解析网页数据

    今天做项目的时候遇到这样一个需求,需要在网页上展示今日黄历信息,数据格式如下 公历时间:2016年04月11日 星期一 农历时间:猴年三月初五 天干地支:丙申年 壬辰月 癸亥日 宜:求子 祈福 开光 ...

  5. Jsoup抓取、解析网页和poi存取excel综合案例——采集网站的联系人信息

    需求:采集网站中每一页的联系人信息 一.创建maven工程,添加jsoup和poi的依赖包 <!-- https://mvnrepository.com/artifact/org.apache. ...

  6. HttpClient+Jsoup 抓取网页信息(网易贵金属为例)

    废话不多说直接讲讲今天要做的事. 利用HttpClient和Jsoup技术抓取网页信息.HttpClient是支持HTTP协议的客户端编程工具包,并且它支持HTTP协议. jsoup 是一款基于 Ja ...

  7. 使用jsoup抓取新闻信息

    1,jsoup简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和 ...

  8. HttpClients+Jsoup抓取笔趣阁小说,并保存到本地TXT文件

    前言 首先先介绍一下Jsoup:(摘自官网) jsoup is a Java library for working with real-world HTML. It provides a very ...

  9. jsoup抓取借书记录

    package tushuguan; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; ...

随机推荐

  1. 泛型、反射和抽象工厂结合解决多DB问题

  2. Python下Pip的安装【get-pip】

    1.下载 下载https://bootstrap.pypa.io/get-pip.py 如果不能下载,可下载:http://files.cnblogs.com/files/zhangzhiming/g ...

  3. Spring boot 开发组件

    一.Jboot 描述:Jboot是一个基于jfinal 和 undertow开发的微服务框架.提供了AOP.RPC.分布式缓存.限流.降级.熔断.统一配置中心.swagger api自动生成.Open ...

  4. HttpMessageConverter和ContentNegotiatingViewResolver

    HttpMessageConverter 在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,HttpMessa ...

  5. LIS和LCS LCIS

    首先介绍一下LIS和LCS的DP解法O(N^2) LCS:两个有序序列a和b,求他们公共子序列的最大长度 我们定义一个数组DP[i][j],表示的是a的前i项和b的前j项的最大公共子序列的长度,那么由 ...

  6. C#序列化效率对比

    原文出处:https://www.cnblogs.com/landeanfen/p/4627383.html 从使用序列化到现在,用到的无非下面几种方式:(1)JavaScriptSerializer ...

  7. php 导出csv表格文件

    1.数据库取出数据,存放在二维数组中 $conn=new mysqli('localhost','root','root','myDBPDO'); $result=$conn->query('s ...

  8. 编写高质量代码改善C#程序的157个建议——建议99:重写时不应使用子类参数

    建议99:重写时不应使用子类参数 重写时,如果使用了子类参数,可能会偏离设计者的预期目标.比如,存在一个如下继承体系: class Employee { } class Manager : Emplo ...

  9. Finding Memory Leaks with SAP Memory Analyzer

    Introduction There is a common understanding that a single snapshot of the java heap is not enough f ...

  10. 大致(主要hadoop)软件下载地址

    jdk-8u181-linux-x64.tar.gz https://www.oracle.com/technetwork/java/javase/downloads/index.html hadoo ...