依赖:
<!--jsoup-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
code:
public static String parseip(String ip) throws IOException {
String ans;
String USERAGENT =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";
String url = "https://www.ip138.com/iplookup.asp?ip=" + ip + "&action=2";
Document doc = Jsoup.connect(url).timeout(3000).userAgent(USERAGENT).get();
if (doc != null) {
Element ulHtml = doc.selectFirst(".ul1");
if (ulHtml != null) {
Elements liHtml = ulHtml.select("li");
if (liHtml != null && liHtml.size() > 0) {
Element first = liHtml.first();
String[] split = first.text().split(":");
ans = split[1];
// for (String s : split) {
// System.out.println(s);
// }
} else {
return "";
}
} else {
return "";
}
} else {
return "";
}
return ans;
}
ASN归属地
北京市北京市 阿里云
北京市北京市 阿里云