I'm very new to programming, so sorry if this is a dumb question. Is it possible to write a program that can view the source of a webpage and extract and re-write a certain part of the code? For example, I would want the program to view the source of this webpage and write whats in between the <title></title> tags which is "Contribute to the Java Forum | DaniWeb." I don't think this is PHP related because I think although the program is reading from a website, the program itself would be external, not on the website. Thanks in advance for any answers I get.

Recommended Answers

All 3 Replies

Yes, just like a browser requests the page, you can write a program to do the same. When you ask about rewriting, you do realize that you wouldn't be changing the actual website page. If you elaborate on what you are trying to achieve that would be helpful in getting better recommendations.

I understand, I would just want to copy the data and have it rewritten in a document. So essentially, I would run the program, input the webpage URL, and it will read the text inbetween the title tags on the page and automatically paste in in a Microsoft Word document

I was able to find some code that allows me read and write the code from a webpage. The only thing left for me to figure out is how to write only a certain part of the text, for example, the text inbetween the <title></title> tags as I said before. Also, is it possible to have it pasted into an already made Microsoft Word document? All I know is how to paste it into a new text document. Thanks guys. Here's the code I found:

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class UrlReadPageDemo {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.daniweb.com/software-development/java/threads/422066/can-you-make-a-program-to-extract-data-from-a-webpage");

            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            BufferedWriter writer = new BufferedWriter(new FileWriter("data4.txt"));

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                writer.write(line);
                writer.newLine();
            }

            reader.close();
            writer.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.