0

我想使用 Jsoup 解析链接。它工作正常,但是当我作为参数链接传递时,看起来像“http://translate.google.com”(包含超过 1 个点)会生成错误。

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()] );      
    }
}
4

1 回答 1

0

我尝试进入http://translate.google.com并出现用户代理错误。尝试这个; 它为我解决了这个问题:

Document doc = Jsoup
        .connect( url )
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0")
        .get();
于 2012-09-17T20:35:52.447 回答