Write a program that reads all data from a web page and prints all hyperlinks of
ID: 3660081 • Letter: W
Question
Write a program that reads all data from a web page and prints all hyperlinks of the form link text No idea how to even start it besides this... I know you read the contents of a webpage with this: (1) String address = "http://horstmann.com/index.html"; (2) URL pageLocation = new URL(address) (3) Scanner in = new Scanner(pageLocation.openStream()); And then the book says simply read the contents of the web page with the scanner in the usual way. The URL constructor and the openStream method can throw an IOException, so you need to the the main method with throws IOException. The URL class is contained in the java.net packageExplanation / Answer
This example code will read the Google and print its source to the console. Java Code: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class URLExp { public static void main(String[] args) { try { URL google = new URL("http://www.google.com/"); URLConnection yc = google.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc .getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch (Exception e) { e.printStackTrace(); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.