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. ConcurrentHashMap的实现原理与使用

    一.适应ConcurrentHashMap的原因 HashMap存在线程不安全的问题,HashTable效率十分低下,因此,ConcurrentHashMap有了合适的登场机会. (1)HashTab ...

  2. 在OpenSSL中添加自定义加密算法

    一.简介 本文以添加自定义算法EVP_ssf33为例,介绍在OpenSSL中添加自定义加密算法的方法 二.步骤 1.修改crypto/object/objects.txt,注册算法OID,如下: rs ...

  3. linux的“自动化”-乾颐堂CCIE

    linux系统的web网站在运营状态时,我们常需要对网站进行维护,例如查看资源剩余并做出响应.日志分割.数据整理,在特定状态执行特定任务等等,这些都会需要linux能实现自动执行某些任任务.本篇博文介 ...

  4. 解决URL参数中文乱码

    string key = HttpUtility.UrlDecode(Request["key"], Encoding.UTF8);

  5. Nlog- Application Logging in C#

    当你在谷歌搜索 Application  Loggin in C#,排在最前面的是这个 .NET Logging Tools and Libraries ,点击进去你会发现里面收录了不错的日记工具及文 ...

  6. 去除json数据的某些键值对

    假如现在要处理的原始数据是字符串.形式如下: var vJson = { name: "张三", class: "软件工程一班" ,other:"无效 ...

  7. centos7设置、查看、删除环境变量的方法

    centos查看环境变量与设置环境变量在使用过程中很常见,本文整理了一些常用的与环境变量相关的命令,感兴趣的朋友可以参考下希望对你有所帮助 1. 显示环境变量HOME(红色部分代表要输入的命令,不要把 ...

  8. not in查询不出数据问题

    select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where Cu ...

  9. Hadoop权威指南文摘

    第1章 初识Hadoop 1.1 数据!数据! 1.2 数据的存储与分析 HDFS实现数据的存储,MapReduce实现数据额分析与处理 1.3 相较于其他系统的优势 MapReduce是一个批量查询 ...

  10. SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

    一.spring cloud简介 鉴于<史上最简单的Spring Cloud教程>很受读者欢迎,再次我特意升级了一下版本,目前支持的版本为Spring Boot版本2.0.3.RELEAS ...