6

次のコードを使用して Google の結果を取得しようとしています。

Document doc = con.connect("http://www.google.com/search?q=lakshman").timeout(5000).get();

しかし、私はこの例外を受け取ります:

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403,URL=http://www.google.com/search?q=lakshman

403 エラーは、サーバーがアクセスを禁止していることを意味しますが、この URL を Web ブラウザーに問題なく読み込むことができます。Jsoup で 403 エラーが発生するのはなぜですか?

4

6 に答える 6

36

次のように、UserAgent プロパティを HTTP ヘッダーに追加するだけです。

Jsoup.connect(itemUrl)
     .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36")
     .get()
于 2014-03-18T02:44:52.660 に答える
6

Google doesn't allow robots, you couldn't use jsoup to connect google. You can use the Google Web Search API (Deprecated) but the number of requests you may make per day will be limited.

于 2013-12-16T22:01:25.250 に答える
1

これを試して:

Document doc =con.connect("http://www.google.com/search?q=lakshman").ignoreHttpErrors(true).timeout(5000).get();

userAgent が機能しなかった場合は、私にとって機能しなかったのと同じように。

于 2016-08-23T23:11:18.407 に答える
1

場合によっては、リファラーを設定する必要があります。私の場合は助かりました。

完全なソースはこちら

    try{

        String strText = 
                Jsoup
                .connect("http://www.whatismyreferer.com")
                .referrer("http://www.google.com")
                .get()
                .text();

        System.out.println(strText);

    }catch(IOException ioe){
        System.out.println("Exception: " + ioe);
    }
于 2018-01-29T21:13:48.000 に答える