Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit 937775d

Browse files
committed
新增xpath解析
1 parent 517174a commit 937775d

File tree

6 files changed

+179
-13
lines changed

6 files changed

+179
-13
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ String msg = Dialogs.showInputDialog("title", "header", "content", "default valu
275275
| dateToLocalDate | 将java.util.Date转换为java.time.LocalDate |
276276
| listToJson | 将List转换为JSON |
277277
| mapToJson | 将Map转换为JSON |
278+
|getFileSuffix|获取文件后缀名|
278279

279280
**10. `com.zhazhapan.util.Utils`**
280281

@@ -414,6 +415,17 @@ BeanUtils.jsonPutIn(jsonObject, bean);
414415
|getDataOfUrl|获取URL对应的网页内容|
415416
|getInputStreamOfUrl|获取URL对应的InputStream对象|
416417
|getInputStreamOfConnection|获取HttpURLConnection对应的InputStream对象|
418+
|evaluate|XPath解析HTML内容|
419+
|getHtmlFromUrl|获取网页内容|
420+
|getDocumentFromUrl|获取HTML文档|
421+
|getComputerName|获取计算机名|
422+
|getSystemName|获取系统名称|
423+
|getSystemArch|获取系统架构|
424+
|getSystemVersion|获取系统版本|
425+
|getMacAddress|获取Mac地址|
426+
|getPublicIpAndLocation|获取公网IP和归属地|
427+
|getLocalIp|获取本地ip地址|
428+
|getLocationByIp|获取ip归属地|
417429

418430
**17. `com.zhazhapan.util.ReflectUtils`**
419431

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@
7575
<artifactId>hutool-all</artifactId>
7676
<version>4.0.9</version>
7777
</dependency>
78+
<dependency>
79+
<groupId>org.jsoup</groupId>
80+
<artifactId>jsoup</artifactId>
81+
<version>1.11.2</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>javax.xml</groupId>
85+
<artifactId>jaxp-api</artifactId>
86+
<version>1.4.2</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>net.sourceforge.htmlcleaner</groupId>
90+
<artifactId>htmlcleaner</artifactId>
91+
<version>2.21</version>
92+
</dependency>
7893
</dependencies>
7994

8095
<profiles>

src/main/java/com/zhazhapan/modules/constant/ValueConsts.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
public class ValueConsts {
1313

14+
/**
15+
* 获取ip归属地的xpath
16+
*/
17+
public static final String IP_REGION_XPATH = "//div[@class='WhoIpWrap jspu']/p[2]/span[4]";
18+
1419
/**
1520
* 公网ip查询地址
1621
*/
@@ -165,7 +170,7 @@ public class ValueConsts {
165170
* 请求头
166171
*/
167172
public static final String[] USER_AGENT = {"mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, " +
168-
"" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "like" + "" + " " + "gecko) " +
173+
"" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "like" + "" + " " + "gecko) " +
169174
"chrome/59.0.3071.115 " + "safari/537.36", "Mozilla/5.0 " + "" + "(Windows NT " + "" + "6.1; " + "" + ""
170175
+ "" + "Win64; " + "x64;" + " " + "rv:47.0)" + "" + " " + "" + "Gecko/20100101" + "" + " " +
171176
"Firefox/47.0", "Mozilla/5.0 " + "" + "" + "" + "(Macintosh;" + "Intel " + "" + "Mac OS X" + " " + "" +
@@ -176,12 +181,13 @@ public class ValueConsts {
176181
"Gecko) " + "" + "Chrome/51.0.2704.106" + "" + "" + " " + "Safari/537.36 " + "OPR/38.0.2220.41",
177182
"Mozilla/5.0 " + "" + "" + "" + "" + "" + "" + "" + "" + "(iPhone;" + "" + " " + "CPU" + " " + "iPhone" +
178183
" " + "" + "" + "OS " + "10_3_1 " + "like" + "" + " Mac " + "" + "" + "OS " + "" + "" + "" + "X) " +
179-
"" + "AppleWebKit/603.1.30 " + "" + "" + "(KHTML, " + "like " + "Gecko) " + "Version/10.0" + " "
180-
+ "Mobile/14E304 " + "Safari/602.1", "Mozilla/5.0" + " " + "" + "" + "(compatible;" + "" + " " +
181-
"MSIE " + "9.0; " + "Windows Phone " + "OS" + " 7.5;" + "" + " " + "Trident/5.0;" + "" + "" + "" + "" +
182-
"" + "" + "" + " " + "IEMobile/9.0)", "Mozilla/5.0 " + "" + "" + "" + "(iPhone; U; CPU " + "like " + "" +
183-
"" + "Mac" + "" + " OS " + "X;" + " " + "en)" + " " + "AppleWebKit/420+ " + "" + "" + "" + "(KHTML," + ""
184-
+ " like " + "" + "Gecko) " + "Version/3.0 " + "" + "Mobile/1A543a" + " " + "Safari/419.3"};
184+
"" + "" + "" + "AppleWebKit/603.1.30 " + "" + "" + "(KHTML, " + "like " + "Gecko) " +
185+
"Version/10.0" + " " + "Mobile/14E304 " + "Safari/602.1", "Mozilla/5.0" + " " + "" + "" + "" +
186+
"(compatible;" + "" + " " + "" + "MSIE " + "9.0; " + "Windows Phone " + "OS" + " 7.5;" + "" + " " +
187+
"Trident/5.0;" + "" + "" + "" + "" + "" + "" + "" + " " + "IEMobile/9.0)", "Mozilla/5.0 " + "" + "" + ""
188+
+ "(iPhone; U; CPU " + "like " + "" + "" + "Mac" + "" + " OS " + "X;" + " " + "en)" + " " +
189+
"AppleWebKit/420+ " + "" + "" + "" + "(KHTML," + "" + " like " + "" + "Gecko) " + "Version/3.0 " + "" +
190+
"Mobile/1A543a" + " " + "Safari/419.3"};
185191

186192
public static final int DEFAULT_DOWNLOAD_REMAINING_CENTER = 1048576;
187193

src/main/java/com/zhazhapan/util/NetUtils.java

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
import com.zhazhapan.config.JsonParser;
44
import com.zhazhapan.modules.constant.ValueConsts;
5-
5+
import org.htmlcleaner.CleanerProperties;
6+
import org.htmlcleaner.DomSerializer;
7+
import org.htmlcleaner.HtmlCleaner;
8+
import org.htmlcleaner.TagNode;
9+
import org.jsoup.Jsoup;
10+
import org.w3c.dom.Document;
11+
12+
import javax.xml.parsers.ParserConfigurationException;
13+
import javax.xml.xpath.XPath;
14+
import javax.xml.xpath.XPathExpressionException;
15+
import javax.xml.xpath.XPathFactory;
616
import java.io.IOException;
717
import java.io.InputStream;
818
import java.net.*;
9-
import java.util.Random;
1019

1120
/**
1221
* @author pantao
@@ -16,6 +25,73 @@ public class NetUtils {
1625

1726
private NetUtils() {}
1827

28+
/**
29+
* 获取ip归属地
30+
*
31+
* @param ip ip地址
32+
*
33+
* @return 归属地
34+
*
35+
* @throws IOException 异常
36+
* @throws XPathExpressionException 异常
37+
* @throws ParserConfigurationException 异常
38+
*/
39+
public static String getLocationByIp(String ip) throws IOException, XPathExpressionException,
40+
ParserConfigurationException {
41+
return evaluate(ValueConsts.IP_REGION_XPATH, getHtmlFromUrl("http://ip.chinaz.com/" + ip));
42+
}
43+
44+
/**
45+
* XPath解析HTML内容
46+
*
47+
* @param xpath xpath表达式
48+
* @param html html内容
49+
*
50+
* @return 解析结果
51+
*
52+
* @throws XPathExpressionException 异常
53+
* @throws ParserConfigurationException 异常
54+
*/
55+
public static String evaluate(String xpath, String html) throws XPathExpressionException,
56+
ParserConfigurationException {
57+
HtmlCleaner hc = new HtmlCleaner();
58+
TagNode tn = hc.clean(html);
59+
Document document = new DomSerializer(new CleanerProperties()).createDOM(tn);
60+
XPath xPath = XPathFactory.newInstance().newXPath();
61+
return xPath.evaluate(xpath, document);
62+
}
63+
64+
/**
65+
* 获取网页内容
66+
*
67+
* @param url 链接
68+
*
69+
* @return {@link String}
70+
*
71+
* @throws IOException 异常
72+
*/
73+
public static String getHtmlFromUrl(String url) throws IOException {
74+
return getDocumentFromUrl(url).html();
75+
}
76+
77+
/**
78+
* 获取HTML文档
79+
*
80+
* @param url 链接
81+
*
82+
* @return {@link org.jsoup.nodes.Document}
83+
*
84+
* @throws IOException 异常
85+
*/
86+
public static org.jsoup.nodes.Document getDocumentFromUrl(String url) throws IOException {
87+
return Jsoup.connect(url).get();
88+
}
89+
90+
/**
91+
* 获取计算机名
92+
*
93+
* @return 计算机名
94+
*/
1995
public static String getComputerName() {
2096
return System.getenv().get("COMPUTERNAME");
2197
}
@@ -67,7 +143,7 @@ public static String getMacAddress() throws UnknownHostException, SocketExceptio
67143
}
68144

69145
/**
70-
* 获取公网IP
146+
* 获取公网IP和归属地
71147
*
72148
* @return 公网ip、address,如:{"ip":"127.0.0.1","address":"you ip location"}
73149
*/
@@ -162,8 +238,7 @@ public static InputStream getInputStreamOfUrl(URL url) throws IOException {
162238
public static InputStream getInputStreamOfConnection(HttpURLConnection connection) throws IOException {
163239
connection.setConnectTimeout(1000 * 6);
164240
connection.setRequestProperty("Charset", "UTF-8");
165-
connection.setRequestProperty("User-Agent", ValueConsts.USER_AGENT[new Random().nextInt(ValueConsts
166-
.USER_AGENT.length)]);
241+
connection.setRequestProperty("User-Agent", ValueConsts.USER_AGENT[0]);
167242
connection.setRequestProperty("Connection", "Keep-Alive");
168243
connection.setRequestProperty("Accept", "*/*");
169244
return connection.getInputStream();

src/main/java/com/zhazhapan/util/dialog/Dialogs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static GridPane getGridPane() {
6464
* @return {@link Dialog}
6565
*/
6666
public static Dialog<String[]> getDialog(String title, ButtonType ok) {
67-
Dialog<String[]> dialog = new Dialog<String[]>();
67+
Dialog<String[]> dialog = new Dialog<>();
6868
dialog.setTitle(title);
6969
dialog.setHeaderText(null);
7070

src/test/java/com/zhazhapan/util/NetUtilsTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,68 @@
22

33
import org.junit.Test;
44

5+
import javax.xml.parsers.ParserConfigurationException;
6+
import javax.xml.xpath.XPathExpressionException;
7+
import java.io.IOException;
8+
59
public class NetUtilsTest {
610

711
@Test
812
public void getComputerName() {
913
System.out.println(NetUtils.getComputerName());
1014
}
15+
16+
@Test
17+
public void evaluate() throws XPathExpressionException, IOException, ParserConfigurationException {
18+
String html = NetUtils.getHtmlFromUrl("http://ip.chinaz.com/125.69.41.90");
19+
System.out.println(NetUtils.evaluate("//div[@class='WhoIpWrap jspu']/p[2]/span[4]", html));
20+
}
21+
22+
@Test
23+
public void getSystemName() {
24+
}
25+
26+
@Test
27+
public void getSystemArch() {
28+
}
29+
30+
@Test
31+
public void getSystemVersion() {
32+
}
33+
34+
@Test
35+
public void getMacAddress() {
36+
}
37+
38+
@Test
39+
public void getPublicIpAndLocation() {
40+
}
41+
42+
@Test
43+
public void getLocalIp() {
44+
}
45+
46+
@Test
47+
public void urlToString() {
48+
}
49+
50+
@Test
51+
public void getDataOfUrl() {
52+
}
53+
54+
@Test
55+
public void getDataOfUrl1() {
56+
}
57+
58+
@Test
59+
public void getInputStreamOfUrl() {
60+
}
61+
62+
@Test
63+
public void getInputStreamOfUrl1() {
64+
}
65+
66+
@Test
67+
public void getInputStreamOfConnection() {
68+
}
1169
}

0 commit comments

Comments
 (0)