主要是通过使用jsoup请求ip138,然后解析网页上的数据。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Test {
private static final String IP_URL = "http://www.ip138.com/ips138.asp?ip=116.116.116.116&action=2";
public static void main(String[] args) {
try {
Document doc = Jsoup.connect(IP_URL)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36") // 设置 User-Agent
.timeout(3000)
.get();
System.out.println(doc.select("body > table > tbody > tr:nth-child(3) > td > ul > li:nth-child(1)").text());
} catch (Exception e) {
e.printStackTrace();
}
}
}
doc.select("body > table > tbody > tr:nth-child(3) > td > ul > li:nth-child(1)"
内填写的路径,在谷歌浏览器F12开发者模式下,可以通过copy selector获得
导入jsoup包即可
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
网友评论