-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay17.java
More file actions
142 lines (109 loc) · 4.54 KB
/
Day17.java
File metadata and controls
142 lines (109 loc) · 4.54 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
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 17 : Challenge IX - Writing in a File (data coming from the web)
// Day 17 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.util.List;
import org.htmlunit.WebClient;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlPage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
@SuppressWarnings("unchecked")
public class Day17 {
private static final String PATH = "text_data/";
public static void main(String[] args)
{
//initialize the url of the website you want to visit
String baseUrl = "https://www.stuntbusiness.ca/fr/legal/terms-and-conditions" ;
// WebClient : The main starting point in HtmlUnit: this class simulates a web browser.
// https://htmlunit.sourceforge.io/apidocs/com/gargoylesoftware/htmlunit/WebClient.html
WebClient client = new WebClient();
// deactivate css and javascript
client.getOptions().setJavaScriptEnabled(false);
client.getOptions().setCssEnabled(false);
// Start parsing your page
try {
// get your html page
HtmlPage page = client.getPage(baseUrl);
// Get all the html element h4 with class="title-in-content"
List<HtmlElement> items = (List<HtmlElement>) (Object) page.getByXPath("//h5[@class='title-description']") ;
if(items.isEmpty())
{
System.out.println("Nothing found with your request.");
}
else{
File directory = new File(PATH);
// create directory if it does not exist
if( !(directory.exists()))
{
directory.mkdir();
}
File file = new File(PATH + "Day17_data.txt");
// creating the file if it does not exist
if(!file.exists())
{
try
{
if(file.createNewFile())
{
System.out.println("\nFile successfully created : "+file.getName());
}
}catch(IOException e)
{
e.printStackTrace();
}
}
else // the file does not exist
{
System.out.println(
String.format("\nFile %s already exists.", file.getName())
);
}
// writing and reading processes
try
{
System.out.println("\n>>>> GETTING ALL H5 TAGS <<<<\nfrom: "+ baseUrl +"\n");
// Write inside the file
FileWriter writer = new FileWriter(file.getAbsolutePath());
System.out.println("\n----------- WRITING ------------\n");
writer.write("\nTag Id\t\tTag Content\n");
// print all the elements collected
int index = 0;
// print the h4 found.
for(HtmlElement htmlItem : items){
writer.write(
String.format("%d\t\t%s\n",index++,htmlItem.asNormalizedText())
);
// System.out.println(
// "> " + (index++) + ":\n" + htmlItem.asText()
// );
}
writer.close();
Scanner sc = new Scanner(file);
System.out.println("\n----------- READING ------------\n");
// read the content of the file
while (sc.hasNextLine())
System.out.println(sc.nextLine());
sc.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
// close your web client
client.close();
} catch(Exception e){
e.printStackTrace();
}
System.out.println("End of program.");
}
}