Jsoup.parse Mobile url



我是 Jsoup 的新手,我正在尝试使用 Jsoup.parse() 下载一个移动网站。下面的代码适用于普通 URL,但不适用于移动设备。怎么了?

法典:

 private static Document downloadDocument(String url, String referer, int timeout) {
    if (url.isEmpty() || url == null) {
        return null;
    }
    if (referer.isEmpty() || referer == null) {
        //default to google.
        referer = "http://www.google.com";
    }
    Document document;
    try {
        document = Jsoup.parse(new URL(url), timeout);
    } catch (IOException e) {
        //TODO - Remove System.out.println - Memory Issue.
        System.out.println("Sorry, unable to download document");
        return null;
    }
    return document;
}

堆栈跟踪如下:

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=http://m.careerbuilder.com/
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
    at org.jsoup.Jsoup.parse(Jsoup.java:183)
    ...

您要解析的网站会检查用户代理,并且不接受默认代理(即 Java/jdk_version)。因此,您应该使用"假"用户代理,如下所示:

Document html = Jsoup.connect("http://m.careerbuilder.com").userAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36").get();
System.out.println(html);

其中 Mozilla/5.0 (Windows NT 6.2;赢64;x64) AppleWebKit/537.36 (KHTML,如壁虎) Chrome/32.0.1667.0 Safari/537.36 是 Chrome 32.0.1667.0 的用户代理

最新更新