Question
Consider the following given code in Java: import java.util.Scanner; import java.io.InputStreamReader; import java.util.ArrayList; public class WebCrawler { public static void main(String[] args) { Scanner input
Consider the following given code in Java:
import java.util.Scanner; import java.io.InputStreamReader;
import java.util.ArrayList;
public class WebCrawler { public static void main(String[] args) {
Scanner input = new Scanner(System.in); String url = " www... ";
crawler(url);
}
public static void crawler(String startingURL) {
ArrayList listOfPendingURLs = new ArrayList<>();
ArrayList listOfTraversedURLs = new ArrayList<>();
listOfPendingURLs.add(startingURL);
while (!listOfPendingURLs.isEmpty() &&
listOfTraversedURLs.size() <= 100) { String urlString = listOfPendingURLs.remove(0);
listOfTraversedURLs.add(urlString);
System.out.println("Crawl " + urlString);
for (String s: getSubURLs(urlString)) {
if (!listOfTraversedURLs.contains(s) && !listOfPendingURLs.contains(s))
listOfPendingURLs.add(s);
}
}
}
public static ArrayList getSubURLs(String urlString) {
ArrayList list = new ArrayList<>();
try { java.net.URL url = new java.net.URL(urlString);
java.net.URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
java.io.InputStreamReader inStream = new java.io.InputStreamReader(connection.getInputStream(), java.nio.charset.Charset.forName("UTF- 8"));
Scanner input = new Scanner(inStream);
int current = 0; while (input.hasNext()) {
String line = input.nextLine(); current = line.indexOf("http", current);
while (current > 0) {
int endIndex = line.indexOf("\"", current); if (endIndex > 0) { // Ensure that a correct URL is found
list.add(line.substring(current, endIndex));
current = line.indexOf("http", endIndex); }
else current = -1;
} }
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage()); }
return list; }
}
Question: modify and add needed code to the crawler program to search e-mail addresses and web pages that contain certain types of images. Your web crawler should visit at least 2000 pages. This web crawler program should search web pages that belongs to the following (any kind of website)...: String[] myURLs = { "..." }; . The web crawler should search e-mail addresses from web pages and record into a text file named emailAddresses.txt. Also, we should record the addresses of images that a web pages contains images that ends with .jpg, .jpeg, .png. The program should record the image addresses into a file named WebImages.txt.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started