Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

1. Explain the 2nd world war. 2. Who is the father of history?

Answered: 1 week ago

Question

Describe key customer feedback collection tools.

Answered: 1 week ago

Question

Know what customers expect from the firm when they complain.

Answered: 1 week ago

Question

Understand why customers complain.

Answered: 1 week ago