简体   繁体   中英

Jsoup connection to the link

I want to parse links using Jsoup. It works normal, but when i pass as parameter link which look like "http://translate.google.com" (contains more than 1 dot) error is generated.

public class HtmlProcessor {

    public String[] getLinks(String url) throws IOException {

        Vector <String> hrefs = new Vector <String> ();

        try {

            Document doc = Jsoup.connect( url ).get();
            Elements links = doc.getElementsByTag("a");


            for (Element link : links) {

                hrefs.add(  link.attr( "href" ) );
            }
        } catch (ConnectException ex) {
            System.out.println(ex.getMessage());
        }

        return hrefs.toArray( new String [hrefs.size()] );      
    }
}

I tried going into http://translate.google.com and came out with a user agent error. Try this; it fixed the problem for me:

Document doc = Jsoup
        .connect( url )
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0")
        .get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM