This commit is contained in:
2026-05-09 03:50:38 +03:30
parent 7ffa76fd45
commit 0aa0a60e74
5 changed files with 70 additions and 39 deletions
+17 -15
View File
@@ -70,11 +70,23 @@ The primary objectives of this assignment are:
Utilize JSoup to parse the HTML file located in the resources folder at this path: `src/main/resources/Movies.html` and extract relevant information such as: Utilize JSoup to parse the HTML file located in the resources folder at this path: `src/main/resources/Movies.html` and extract relevant information such as:
- `<span class="movie-title">` = for movie titles - `<h3 class="movie-title">` = for movie titles
- `<span class="movie-rating">` = for movie ratings - `<span class="movie-rating">` = for movie ratings
- `<span class="movie-year">` = for movie release years - `<span class="movie-year">` = for movie release years
**Note: The span tags are nested within other tags and div closures. Look out for unneeded text like '/10' inside elements.** **Note: The elements are nested within other tags and div closures. Look out for unneeded text like '/10' inside elements.**
Here is a general structure of the HTML you will be working with:
```html
<div class="col-md-4 movie">
<h3 class="movie-title">The Godfather</h3>
<div class="movie-info">
<strong>Rating:</strong> <span class="movie-rating">9.2/10</span><br>
<strong>Year:</strong> <span class="movie-year">1972</span><br>
</div>
</div>
```
5. **Implement Sorting Functionality:** 5. **Implement Sorting Functionality:**
@@ -89,15 +101,7 @@ The primary objectives of this assignment are:
7. **Visualize Results:** 7. **Visualize Results:**
- Clearly present the sorted movie data. - Clearly present the sorted movie data using the `Main` class you implement.
## Bonus Objectives (For Advanced Users) 🌟
To enhance your project further:
- Expand data extraction to additional websites, such as [Oscar Winning Films](https://www.scrapethissite.com/pages/ajax-javascript/).
- Develop a user interface for interactive querying and sorting.
- Utilize SQL or NoSQL databases to store the scraped data.
## Notes 📝 ## Notes 📝
@@ -106,7 +110,6 @@ Here are some important points to keep in mind:
- There is a `Help` folder located at the root directory of the project which contains the sorted movies each by different priorities. It is there just so you can see what your output should look like in the end. - There is a `Help` folder located at the root directory of the project which contains the sorted movies each by different priorities. It is there just so you can see what your output should look like in the end.
- The unit tests are provided to assist you in understanding the project requirements. Your final grade is not solely dependent on their results; they are meant to aid your learning process. Remember to enable GitHub Actions for the test workflow to run on GitHub. - The unit tests are provided to assist you in understanding the project requirements. Your final grade is not solely dependent on their results; they are meant to aid your learning process. Remember to enable GitHub Actions for the test workflow to run on GitHub.
- Feel free to leverage ChatGPT for learning web scraping and resolving any challenges that you may not find solutions to on the internet. Utilize its capabilities to enhance your understanding and overcome obstacles effectively. - Feel free to leverage ChatGPT for learning web scraping and resolving any challenges that you may not find solutions to on the internet. Utilize its capabilities to enhance your understanding and overcome obstacles effectively.
**But it is strictly prohibited to use ChatGPT or any other AI generative model for completing any section of this assignment. Failure to comply will result in a score of 0 without any warnings.**
## Evaluation 🧐 ## Evaluation 🧐
@@ -132,10 +135,9 @@ If you have any further questions or need clarification, do not hesitate to reac
## TA Tutorial Videos 🎥 ## TA Tutorial Videos 🎥
To help you succeed in this assignment, our TA team has recorded dedicated tutorial videos covering the essential topics. Please watch these before starting: To help you succeed in this assignment, our TA team has recorded dedicated tutorial videos covering the essential topics. Please watch these before starting. You can access the folder containing the tutorial videos here:
- [Tutorial: Basics of HTML & CSS]([INSERT_LINK_HERE]) - [TA Tutorial Videos - Movies Folder](https://drive.meshcomp.ir/d/3e149a8347a841f6bb81/)
- [Tutorial: Web Scraping in Java using JSoup]([INSERT_LINK_HERE])
## Additional Resources 📚 ## Additional Resources 📚
+22
View File
@@ -0,0 +1,22 @@
import java.io.IOException;
public class Main {
public static void main(String[] args) {
// The relative path automatically points to the resources folder
String filePath = "src/main/resources/Movies.html";
try {
// Instantiate the parser
Parser parser = new Parser(filePath);
// TODO: You can test your code here before you run the unit tests.
// Example: Call your sorting methods, iterate through the returned list,
// and print out the movies to see if they match the expected results in the Help folder.
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size());
} catch (IOException e) {
System.err.println("Error reading or parsing the HTML file: " + e.getMessage());
}
}
}
+5 -3
View File
@@ -6,7 +6,8 @@ public class Movie {
private int year; private int year;
public Movie(String title, double rating, int year) { public Movie(String title, double rating, int year) {
//TODO // TODO: Initialize the instance variables (this.title, this.rating, this.year)
// with the values passed as arguments to this constructor.
} }
public String getTitle() { public String getTitle() {
@@ -23,7 +24,8 @@ public class Movie {
@Override @Override
public String toString() { public String toString() {
//TODO // TODO: Return a string representation of the movie matching the exact required format.
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
return ""; return "";
} }
@@ -34,4 +36,4 @@ public class Movie {
Movie movie = (Movie) o; Movie movie = (Movie) o;
return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title); return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title);
} }
} }
+24 -19
View File
@@ -7,41 +7,46 @@ import java.io.IOException;
import java.util.*; import java.util.*;
public class Parser { public class Parser {
static List<Movie> movies = new ArrayList<>(); private List<Movie> movies = new ArrayList<>();
public Parser(String filePath) throws IOException {
setUp(filePath);
}
public List<Movie> sortByTitle() { public List<Movie> sortByTitle() {
List<Movie> sortedByTitle = new ArrayList<>(movies); List<Movie> sortedByTitle = new ArrayList<>(movies);
// Sort movies alphabetically by title // TODO: Sort the 'sortedByTitle' list alphabetically by the movie's title.
//TODO // Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator.
// Example: Comparator.comparing(Movie::getTitle)
return sortedByTitle; return sortedByTitle;
} }
public List<Movie> sortByRating() { public List<Movie> sortByRating() {
List<Movie> sortedByRating = new ArrayList<>(movies); List<Movie> sortedByRating = new ArrayList<>(movies);
// Sort movies by rating (highest to lowest) // TODO: Sort the 'sortedByRating' list by the movie's rating in descending order (highest to lowest).
//TODO // Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed().
return sortedByRating; return sortedByRating;
} }
public List<Movie> sortByYear() { public List<Movie> sortByYear() {
List<Movie> sortedByYear = new ArrayList<>(movies); List<Movie> sortedByYear = new ArrayList<>(movies);
// Sort movies by year (newest to oldest) // TODO: Sort the 'sortedByYear' list by the movie's release year in descending order (newest to oldest).
//TODO // Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
return sortedByYear; return sortedByYear;
} }
public void setUp() throws IOException { private void setUp(String filePath) throws IOException {
// Parse the HTML file using Jsoup // TODO: Create a java.io.File object using the given 'filePath'.
//TODO // TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object.
// Extract data from the HTML // TODO: Extract the list of movie elements from the Document.
//TODO // Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
// Iterate through each movie div to extract movie data // TODO: Iterate through each movie Element.
//TODO // For each movie element:
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text.
// 2. Find the rating element (e.g., using selector "span.movie-rating"). Extract the text, remove the "/10" part, and parse it to a double.
// 3. Find the year element (e.g., using selector "span.movie-year"). Extract the text and parse it to an int.
// 4. Create a new Movie object with these values and add it to the 'movies' list.
} }
}
public static void main(String[] args) {
// You can test your code here before you run the unit tests
}
}
+2 -2
View File
@@ -12,8 +12,8 @@ public class ParserTest {
@BeforeAll @BeforeAll
static void setUp() throws IOException { static void setUp() throws IOException {
handle = new Parser(); String filePath = "src/main/resources/Movies.html";
handle.setUp(); handle = new Parser(filePath);
} }
@Test @Test