Results 1 to 3 of 3
Thread: Problems Reading HTML
- 10-11-2012, 01:57 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Problems Reading HTML
Hi all, I've been asked to write a program that pulls links out of HTML code for a pretty basic website and prints them out, I'm just starting and am trying to print out all the lines that contain the string "href" and am having some trouble. Heres the code:
my understanding is that the program should read through the HTML code line by line and print the lines that contain "href" but I don't get any returns or an error, so I'm assuming b is always false. Can anyone explain what I'm doing wrong? Also I'm sorry if this belongs in advanced java instead of new to java, I was unsure where it falls and since I have only been programming for a year I thought it would be best fit here, thanks.Java Code:import java.net.*; import java.io.*; public class readURLTest { public static void main(String args[]) { try { URL interisle = null; DataInputStream dis = null; interisle = new URL("http://www.interisle.net"); dis = new DataInputStream(interisle.openStream()); String line = dis.readLine(); String ref = "href"; boolean b = line.contains(ref); while (line != null) { line = dis.readLine(); if(b==true) { System.out.println(line); } } } catch (IOException e) { System.out.println("Error:" + e.getMessage()); } } }
-
Re: Problems Reading HTML
For my money, I'd use a library that makes this easy to do, one that parses the HTML for you such as the wonderful JSoup.
- 10-11-2012, 09:47 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Problems Reading HTML
You only check the first line read in for 'href'.Java Code:String line = dis.readLine(); String ref = "href"; boolean b = line.contains(ref); while (line != null) { line = dis.readLine(); if(b==true) { System.out.println(line); } }
That boolean never changes after that.Please do not ask for code as refusal often offends.
Similar Threads
-
reading urls from html file.
By fishy8158 in forum New To JavaReplies: 2Last Post: 11-20-2011, 06:21 AM -
Reading values between HTML tags.
By bholzer in forum New To JavaReplies: 6Last Post: 05-03-2011, 02:25 AM -
problems with html parser
By vitaly87 in forum Advanced JavaReplies: 0Last Post: 03-13-2010, 01:37 PM -
reading an Html file and checking for urls
By sudukrish in forum Advanced JavaReplies: 1Last Post: 04-25-2009, 01:39 AM -
Help in reading values from html form in java
By ichkoguy in forum Advanced JavaReplies: 7Last Post: 03-16-2009, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks