- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
169 lines (142 loc) · 6.69 KB
/
Copy pathMain.java
File metadata and controls
169 lines (142 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.nodes.Element;
importorg.jsoup.select.Elements;
importCrawl.LinkEvent;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.List;
importjava.util.ArrayList;
importjava.util.LinkedList;
importjava.util.Map;
importjava.io.FileWriter;
publicclassMain {
publicstaticbooleancheckOneLevelUrl(Stringurl) {
try {
// Fetch the HTML content from the URL
Documentdoc = Jsoup.connect(url).get();
// Check if the desired class exists in the parsed document
booleanhasInfoboxVevent = doc.select(".infobox.vevent").size() > 0;
// Print the result
if (hasInfoboxVevent) {
returntrue;
} else {
returnfalse;
}
} catch (IOExceptione) {
returnfalse;
}
}
publicstaticvoidinfobox(Stringurl) throwsIOException {
// Map<String, String> instance = null;
try {
// instance = new HashMap<String, String>(10000);
DocumentdocChild = Jsoup.connect(url).get();
Elementcontent = docChild.getElementsByClass("infobox vevent").first();
Elementdes = content.getElementsMatchingOwnText("Địa điểm").first();
Stringdestination = des.nextElementSibling().text();
Elementre = content.getElementsMatchingOwnText("Kết quả").first();
Stringresult = re.nextElementSibling().text();
ElementtrElement = content.select("tr:has(th:containsOwn(Tham chiến))").first();
Elementarmy = trElement.nextElementSibling();
Elementsthamchien = army.select("a[href]");
List<String> trieudai = newArrayList<>();
for (Elementi : thamchien)
trieudai.add(i.text());
StringtdLienQuan = String.join(", ", trieudai);
ElementtrElement2 = content.select("tr:has(th:containsOwn(Chỉ huy))").first();
Elementleader = trElement2.nextElementSibling();
Elementschihuy = leader.select("a[href]");
List<String> nhanvat = newArrayList<>();
for (Elementi : chihuy)
nhanvat.add(i.text());
StringnvLienQuan = String.join(", ", nhanvat);
ElementtrElement3 = content.select("tr:has(th:containsOwn(Lực lượng))").first();
Elementnumber = trElement3.nextElementSibling();
ElementfirstNB = number.select("td").first();
ElementsecondNB = number.select("td").last();
Stringlucluong1 = "Bên 1 " + firstNB.text();
Stringlucluong2 = "Bên 2 " + secondNB.text();
ElementtrElement4 = content.select("tr:has(th:containsOwn(Thương vong))").first();
Elementdeath = trElement4.nextElementSibling();
ElementfirstD = death.select("td").first();
ElementsecondD = death.select("td").last();
Stringthuongvong1 = "Bên 1 " + firstD.text();
Stringthuongvong2 = "Bên 2 " + secondD.text();
System.out.println("Kết quả: " + result);
System.out.println("Địa điểm liên quan: " + destination);
System.out.println("Triều đại liên quan (Tham chiến): " + tdLienQuan);
System.out.println("Nhân vật liên quan (Chỉ huy): " + nvLienQuan);
System.out.println("Lực lượng: " + lucluong1 + ", " + lucluong2);
System.out.println("Thương vong: " + thuongvong1 + ", " + thuongvong2);
} catch (IOExceptione) {
e.printStackTrace();
}
}
publicstaticStringtheloai(Stringurl) throwsIOException {
StringreturnString = "";
try {
Documentdoc = Jsoup.connect(url).get();
Elementdiv = doc.select("div#mw-normal-catlinks").first();
Elementelementul = div.select("ul").first();
Elementselementli = elementul.select("li");
List<String> theloai = newArrayList<>();
for (Elemente : elementli)
theloai.add(e.text());
returnString = String.join(", ", theloai);
} catch (IOExceptione) {
e.printStackTrace();
}
returnreturnString;
}
publicstaticStringcontentInfoBox(Stringelement) throwsIOException {
StringreturnString = "";
Documentdoc = Jsoup.connect(
"https://vi.wikipedia.org/wiki/Chi%E1%BA%BFn_tranh_H%C3%A1n%E2%80%93Vi%E1%BB%87t_(42%E2%80%9343)")
.get();
Elementcontent = doc.getElementsByClass("infobox vevent").first();
ElementcontentSpecial = content.getElementsMatchingOwnText(element).first();
if (contentSpecial != null && contentSpecial.nextElementSibling() != null) {
returnString = returnString + contentSpecial.nextElementSibling().text();
}
returnreturnString;
}
publicstaticvoidmota(Stringurl) throwsIOException {
Documentdoc = Jsoup.connect(url).get();
Elementdiv = doc.select("div.mw-parser-output").first();
Elementsparagraphs = div.select("p");
ElementpElement = paragraphs.get(0);
System.out.println(pElement);
}
publicstaticvoidlist(Stringurl) throwsIOException {
Documentdoc = Jsoup.connect(url).get();
ElementdivElements = doc.select("div[style=\"padding:0em 0.25em\"]").first();
Elementshref = divElements.select("a");
for (Elementi : href) {
StringlinkUrl = i.attr("abs:href");
//System.out.println(linkUrl);
if (checkOneLevelUrl(linkUrl)) {
System.out.println(linkUrl);
System.out.println(i.text());
}
}
}
publicstaticvoidimage(Stringurl) throwsIOException{
try {
// instance = new HashMap<String, String>(10000);
DocumentdocChild = Jsoup.connect(url).get();
Elementcontent = docChild.getElementsByClass("infobox vevent").first();
Elementimage = content.selectFirst("img");
StringimageUrl = image.attr("abs:src");
}catch (IOExceptione) {
e.printStackTrace();
}
}
publicstaticvoidmain(String[] args) throwsIOException {
// System.out
// .println(theloai("https://vi.wikipedia.org/wiki/Chi%E1%BA%BFn_tranh_T%E1%BA%A7n%E2%80%93Vi%E1%BB%87t"));
//System.out.println(contentInfoBox("Thời gian"));
//list("https://vi.wikipedia.org/wiki/Hi%E1%BB%87p_%C4%91%E1%BB%8Bnh_Paris_1973");
image("https://vi.wikipedia.org/wiki/Chi%E1%BA%BFn_tranh_H%C3%A1n%E2%80%93Nam_Vi%E1%BB%87t");
}
}