Jsoup 解析html

根据地区搜索区号

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]);
        }
*/
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值