簡體   English   中英

使用JSoup遇到錯誤。 為什么?

[英]Getting an error using JSoup. Why?

我正在嘗試從Fantasyfootball網站登錄並提取數據。

我收到以下錯誤,

2015年7月24日8:01:12 PM StatsCollector主要SEVERE:null org.jsoup.HttpStatusException:HTTP錯誤獲取URL。 Status = 403,URL = org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:537)上的org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection。上的http://fantasy.premierleague.com/ org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)處的StatsCollector.main(StatsCollector.java:26)的java:493)

每當我嘗試此代碼。 我要去哪里錯了?

    public class StatsCollector {

    public static void main (String [] args){

        try {
            String url = "http://fantasy.premierleague.com/";
            Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).execute();

            Response res= Jsoup
                    .connect(url)
                    .data("ismEmail", "example@googlemail.com", "id_password", "examplepassword")
                    .method(Method.POST)
                    .execute();


            Map<String, String> loginCookies = res.cookies();

            Document doc = Jsoup.connect("http://fantasy.premierleague.com/transfers")
                    .cookies(loginCookies)
                    .get();

            String title = doc.title();
            System.out.println(title);
        }  

        catch (IOException ex) {
            Logger.getLogger(StatsCollector.class.getName()).log(Level.SEVERE,null,ex);
        }
    }

}
Response res= Jsoup
                .connect(url)
                .data("ismEmail", "example@googlemail.com", "id_password", "examplepassword")
                .method(Method.POST)
                .execute();

您是否要執行此實際代碼? 這似乎是帶有占位符而不是登錄憑據的示例代碼。 這將解釋您收到的錯誤HTTP 403

編輯1

我的錯。 我查看了該站點上的登錄表單,在我看來,您將輸入元素的id (“ ismEmail”和“ id_password”)與通過表單發送的name (“ email”,“ password” ”)。這對您有用嗎?

Response res= Jsoup
                .connect(url)
                .data("email", "example@googlemail.com", "password", "examplepassword")
                .method(Method.POST)
                .execute();

編輯2

好的,這一直困擾着我,因為使用JSoup登錄網站應該不那么困難。 我在那里建立了一個帳戶,並為自己嘗試。 代碼優先:

 String url = "https://users.premierleague.com/PremierUser/j_spring_security_check";

        Response res = Jsoup
                .connect(url)
                .followRedirects(false)
                .timeout(2_000)
                .data("j_username", "<USER>")
                .data("j_password", "<PASSWORD>")
                .method(Method.POST)
                .execute();

        Map<String, String> loginCookies = res.cookies();

        Document doc = Jsoup.connect("http://fantasy.premierleague.com/squad-selection/")
                .cookies(loginCookies)
                .get();

那么這里發生了什么? 首先,我意識到登錄表單的目標是錯誤的。 該網頁似乎要在春建,所以表單屬性和目標使用Spring默認j_spring_security_checkj_usernamej_password 然后我發生了讀取超時,直到我將標志設置為followRedirects(false)為止。 我只能猜測為什么這樣做有幫助,但是也許這是對爬蟲的保護?

最后,我嘗試連接到小隊選擇頁面,解析的響應包含我的個人觀點和數據。 該代碼似乎對我有用,您可以嘗試一下嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM