根据地区搜索区号
http://www.ip138.com/post/search.asp?area=%B9%FE%C3%DC%B5%D8%C7%F8&action=area2zone
解析该网站html获取到区号数据
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
简单解析
public static String findAreaCode(String areaName) throws Exception {
String area = java.net.URLEncoder.encode(areaName, "gbk");
String actionUrl = "http://www.ip138.com/post/search.asp?" + "area=" + area + "&action=area2zone";
Document doc = Jsoup.connect(actionUrl).get();
Element element = doc.body();
Elements elements = element.select("table");
Element e = elements.last();
Elements trs = e.select("tr");
Elements tds = trs.get(1).select("td");
String text = tds.first().text();
String[] strings = text.split(":");
if (strings == null || strings.length <= 2) {
throw new Exception("该地区没有获取到区号:" + URLDecoder.decode(area, "gbk"));
} else {
return strings[2].trim();
}
}
处理业务
package com.wdletu.common.area.util;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URLDecoder;
/**
* Created by zhangmy on 2017/7/21.
*/
public class JSoupUtil {
public static String findAreaCode(String areaName) throws Exception {
String area = java.net.URLEncoder.encode(areaName, "gbk");
String actionUrl = "http://www.ip138.com/post/search.asp?" + "area=" + area + "&action=area2zone";
Document doc = Jsoup.connect(actionUrl).get();
Element element = doc.body();
Elements elements = element.select("table");
Element e = elements.last();
Elements trs = e.select("tr");
String code = "";
if (trs.size() > 3) {
Elements newTrs = new Elements();
//throw new Exception("该地区存在多个区号:"+URLDecoder.decode(area, "gbk"));
for (int i = 0; i < trs.size(); i++) {
if (i != 0 && i < trs.size() - 1) {
Element tr = trs.get(i);
newTrs.add(tr);
}
}
for (int i = 0; i < newTrs.size(); i++) {
Elements tds = newTrs.get(i).select("td");
String text = tds.first().text();
String[] strings = text.split(" ");
if (strings == null || strings.length < 3) {
throw new Exception("一个地区多个区号,未获取到城市名" + URLDecoder.decode(area, "gbk"));
}
String cityName = strings[2];
if (StringUtils.equals(cityName.trim(), areaName.trim())) {
String[] strs = text.split(":");
if (strs == null || strs.length <= 2) {
throw new Exception("一个地区多个区号出,城市名称相同,但未获取到区号:" + URLDecoder.decode(area, "gbk"));
} else {
code = strs[2].trim();
break;
}
}
}
return code;
} else {
Elements tds = trs.get(1).select("td");
String text = tds.first().text();
String[] strings = text.split(":");
if (strings == null || strings.length <= 2) {
throw new Exception("该地区没有获取到区号:" + URLDecoder.decode(area, "gbk"));
} else {
return strings[2].trim();
}
}
}
public static void main(String[] args) throws Exception {
String code = findAreaCode("北京市");
/*System.out.println(findAreaCode ("河南省"));*/
/*if (StringUtils.equals(code.trim(), "0371")) {
System.out.println("aa");
}*/
System.out.println(code);
/* String area = java.net.URLEncoder.encode("河南省", "gbk");
String actionUrl = "http://www.ip138.com/post/search.asp?" + "area=" + area + "&action=area2zone";
Document doc = Jsoup.connect(actionUrl).get();
Element element = doc.body();
Elements elements = element.select("table");
Element e = elements.last();
Elements trs = e.select("tr");
Elements tds = trs.get(1).select("td");
String text = tds.first().text();
String[] strings = text.split(":");
if (strings == null || strings.length <= 2) {
System.out.println("该地区没有获取到区号:" + URLDecoder.decode(area, "gbk"));
} else {
System.out.println(strings[2]);
}
*/
}
}