Search code examples
javaperlweb-scrapingcopy-paste

Java Program or Perl Script to copy text off a webpage


Specifically im wondering if in java (preferred) or perl is it possible to enter a url and have it copy text from that page? Specifically I want to be able to search something on google and just copy paste the first 5 links that come up. Not doing SEO or anything its just for a program im working on.


Solution

  • This can definitely be done in either language. Take a look at the following for java:

    http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html

    From the documentation:

    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(
        new InputStreamReader(
        oracle.openStream()));
    
    String inputLine;
    
    while ((inputLine = in.readLine()) != null)
      System.out.println(inputLine);
    
    in.close();
    

    This will give you the HTML on the page. You will need to parse that as needed to pull out the specific text you are interested in.