Question

I've been working on making a simple program that runs through all the links in a page, and visits them, then recurses. But it seems to stop as soon as its run with the error

java.net.MalformedURLException: no protocol: /intl/en/policies/
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at me.dylan.WebCrawler.WebC.sendGetRequest(WebC.java:67)
at me.dylan.WebCrawler.WebC.<init>(WebC.java:27)
at me.dylan.WebCrawler.WebC.main(WebC.java:36)

My code:

package me.dylan.WebCrawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class WebC {
//  FileUtil f;
    int linkamount=0;
    ArrayList<URL> visited = new ArrayList<URL>();
    ArrayList<String> urls = new ArrayList<String>();
    public WebC() {

        try {
//          f= new FileUtil();
            sendGetRequest("http://www.google.com");
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new WebC();
    }
    public void sendGetRequest(String path) throws IOException, BadLocationException, MalformedURLException {

        URL url = new URL(path);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Language", "en-US");
         BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
         EditorKit kit = new HTMLEditorKit();
         HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
         doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
         kit.read(rd, doc, 0);

         //Get all <a> tags (hyperlinks)
         HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
         while (it.isValid())
         {
             MutableAttributeSet mas = (MutableAttributeSet)it.getAttributes();
             //get the HREF attribute value in the <a> tag
             String link = (String)mas.getAttribute(HTML.Attribute.HREF);
             if(link!=null && link!="") {
                 urls.add(link);
             }

             it.next();
         }
         for(int i=urls.size()-1;i>=0;i--) {
             if(urls.get(i)!=null) {
                if(/*f.searchforString(urls.get(i)) ||*/ visited.contains(new URL(urls.get(i)))) {
                    urls.remove(i);
                    continue;
                } else {
                    System.out.println(linkamount++);
                    System.out.println(path);
                    visited.add(new URL(path));
                    //f.write(urls.get(i));
                    sendGetRequest(urls.get(i));
                }
                 try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
             }
         }           
    }
}

I honestly have no idea how to work around this. Apparently google has a href tag that is not a valid url, how would I work around this?

Was it helpful?

Solution

You must append the baseURl in the URL section. The URL object expects it in format http://abc.com/int/etc/etc.

While the form will have in the format of relative format.Easy way out is to just append the http://www.google.com before calling get in each of the HREFs you get.

OTHER TIPS

A quick fix would be to append urls.get(i) to requestPath before calling. That would give it a protocol and a domain to use. The only catch is that if you don't scan the current url in the loop for a protocol and domain, you may end up like this:

http://www.google.com/http://www.yahoo.com/policies

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top