-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcademicBookSearch.java
More file actions
45 lines (42 loc) · 1.58 KB
/
AcademicBookSearch.java
File metadata and controls
45 lines (42 loc) · 1.58 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
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.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AcademicBookSearch {
public static List<String> getAcademicBooks(String subject) {
List<String> books = new ArrayList<>();
try {
String searchQuery = "academic books on " + URLEncoder.encode(subject, "UTF-8");
String searchUrl = "https://www.google.com/search?q=" + searchQuery;
Document doc = Jsoup.connect(searchUrl).userAgent("Mozilla").get();
Elements results = doc.select("h3");
for (Element result : results) {
books.add(result.text());
}
} catch (UnsupportedEncodingException | IOException e) {
e.printStackTrace();
}
return books;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a subject: ");
String subject = scanner.nextLine();
List<String> academicBooks = getAcademicBooks(subject);
if (!academicBooks.isEmpty()) {
System.out.println("Suggested academic books on " + subject + ":");
for (String book : academicBooks) {
System.out.println(book);
}
} else {
System.out.println("No academic books found on " + subject + ".");
}
scanner.close();
}
}