Question

Q1) WebPageReader.java (40 Points) For this problem, you will use the sample code given in the textbook (Uploaded to Canvas) as a template. Modify the program so that it reads the webpage https://openlibrary.org/ and prints book category title and book category count as a table: No user input is required. Your challenge is understanding the structure of the web page contents and reading the correct data you are interested irn Title : Count Art 37,517 Fantasy 8,696 Biographies 5,326 Science 45,061 Recipes 4,235 Romance 15,112 Religion 40,643 Mystery And Detective Stories 6,666 Music 36,509 Medicine 21,022 Plays 1,479 History 934,627 Children 22,543 Science Fiction: 9,350 Textbooks14,128

For this problem, you will use the sample code given in the textbook (Uploaded to Canvas) as a
template.
Modify the program so that it reads the webpage https://openlibrary.org/ and prints book
category title and book category count as a table:
No user input is required. Your challenge is understanding the structure of the web page
contents and reading the correct data you are interested in.

In Java

sample text

import java.io.IOException;

import java.net.URL;

import java.util.Scanner;

/**

This program prints all lines from a web page that contain

references to other web sites.

*/

public class WebPageReader

{

public static void main(String[] args) throws IOException

{

String address = "http://horstmann.com/index.html";

URL pageLocation = new URL(address);

Scanner in = new Scanner(pageLocation.openStream());

while (in.hasNext())

{

String line = in.next();

if (line.contains("href=\"http://"))

{

int from = line.indexOf("\"");

int to = line.lastIndexOf("\"");

System.out.println(line.substring(from + 1, to));

}

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/** This program prints all lines from a web page that contain references to

* other web sites. */

public class WebPageReader {

public static void main(String[] args) throws IOException {

try {

URL url = new URL("https://openlibrary.org/");

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String inputLine;

  

//formating output into table format

System.out.format("%20s%15s%8s", "Title","|", "Count");

System.out.println("\n-------------------------------------------");

  

// create pattern for split the data from HTML tag

final Pattern patternForTitle = Pattern.compile("<p class=\"category-title\">(.+?)</p>");

final Pattern patternForCount = Pattern.compile("<p class=\"category-count\" name=\"\">(.+?)</p>");

  

//iterate all HTML code

while ((inputLine = in.readLine()) != null) {

//check for books title

if(inputLine.contains("category-title")){

final Matcher matcherForTitle = patternForTitle.matcher(inputLine);

matcherForTitle.find();

  

//print category title

System.out.format("%32s%3s", matcherForTitle.group(1),"|");

}

//check for books count

if(inputLine.contains("category-count")){

final Matcher matcherForCount = patternForCount.matcher(inputLine);

matcherForCount.find();

String count = matcherForCount.group(1).split(" ")[0];

//print books count

System.out.format("%8s", count);

System.out.println();

}

}

in.close();

} catch (MalformedURLException me) {

System.out.println(me);

} catch (IOException ioe) {

System.out.println(ioe);

}

}

}

Output:

Markers Properties ServesConsole 3 Data <terminated> WebPageReader [Java Applicationl C:AProgram FilesJ Title Count Art Fanta

Note: I have used https://openlibrary.org/ url for reading the category and books count.

Add a comment
Know the answer?
Add Answer to:
For this problem, you will use the sample code given in the textbook (Uploaded to Canvas)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

  • Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to...

    Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to Programming Challenge 1 of Chapter 3 of your text, page 184 (187 in Edition 5). Given one of the Roman numerals I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV as an input your program must determine and display the corresponding decimal digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15....

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT