Repository navigation
Expand file tree
/
Copy pathPageDataExtractor.java
More file actions
53 lines (42 loc) · 1.65 KB
/
Copy pathPageDataExtractor.java
File metadata and controls
53 lines (42 loc) · 1.65 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
package crawlingdata;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public abstract class PageDataExtractor {
protected String[] motherUrls;
public abstract Data extractPageData(String url, Document document) throws IOException;
public Set<Data> scrapePages() {
Set<Data> dataset = new HashSet<>();
try {
String[] motherUrls = getMotherUrls();
for (String motherUrl : motherUrls) {
Document motherDoc = Jsoup.connect(motherUrl).get();
Elements links = motherDoc.select("h3.elementor-post__title a");
for (Element link : links) {
String childUrl = link.absUrl("href");
Document childDoc = Jsoup.connect(childUrl).get();
Data data = extractPageData(childUrl, childDoc);
if (data != null) {
dataset.add(data);
// Delay before scraping next page to avoid overloading server
TimeUnit.MILLISECONDS.sleep(1000); // 1 second delay
}
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return dataset;
}
public PageDataExtractor(String[] motherUrls) {
this.motherUrls = motherUrls;
}
protected String[] getMotherUrls() {
return motherUrls;
}
}