天天看點

爬蟲三:小案例模拟登陸慢慢買并擷取使用者積分

public class ReptileLogin {

    @Test
    public void Login() throws Exception{
        //确定url:
        String indexUrl = "http://home.manmanbuy.com/login.aspx";
        //2、發送請求,擷取資料
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(indexUrl);

        //設定請求參數
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("__VIEWSTATE","/wEPDwULLTIwNjQ3Mzk2NDFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQlhdXRvTG9naW4voj01ABewCkGpFHsMsZvOn9mEZg=="));
        params.add(new BasicNameValuePair("__EVENTVALIDATION","/wEWBQLW+t7HAwLB2tiHDgLKw6LdBQKWuuO2AgKC3IeGDJ4BlQgowBQGYQvtxzS54yrOdnbC"));
        params.add(new BasicNameValuePair("txtUser","賬戶名"));
        params.add(new BasicNameValuePair("txtPass","密碼"));
        params.add(new BasicNameValuePair("btnLogin","登入"));

        //設定請求頭資訊
        /*網站加了防盜鍊,必須設定是從哪個網頁跳轉過來的*/
        post.setHeader("Referer","http://home.manmanbuy.com/login.aspx");
        post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");
        //将參數加入請求體
        HttpEntity entity = new UrlEncodedFormEntity(params);
        post.setEntity(entity);
        //執行
        CloseableHttpResponse response = httpClient.execute(post);

        httpClient.close();

        //判斷是否成功跳轉
        if(response.getStatusLine().getStatusCode()==302){
            //擷取響應頭資訊
            Header[] locations = response.getHeaders("Location");
            //System.out.println(locations[0].getValue());
            /*跳轉之後的url:http://home.manmanbuy.com/usercenter.aspx
            * locations[0].getValue():usercenter.aspx
            * */
            //拼接新的url
            String new_url = "http://home.manmanbuy.com"+locations[0].getValue();
            //拿到響應頭中Set-Cookie的值
            Header[] cookies = response.getHeaders("Set-Cookie");

            //重新發送請求,擷取資料
            httpClient = HttpClients.createDefault();
            HttpGet get = new HttpGet(new_url);

            //重新設定請求頭資訊(因為url變,網頁重定向,需要手動設定使用者cookie資訊)
            //Cookie的預設寫法,[0]為隐藏,[1]可以直接看到
            get.setHeader("Cookie",cookies[0].getValue()+" "+cookies[1].getValue());

            CloseableHttpResponse execute_info = httpClient.execute(get);
            String info = EntityUtils.toString(execute_info.getEntity(), "gb2312");

            /*-----------------------------------------------------------*/
            //解析資料
            Document parse = Jsoup.parse(info);
            Element element = parse.select("#aspnetForm > div.udivright > div:nth-child(2) > table > tbody > tr > td:nth-child(1) > table:nth-child(2) > tbody > tr > td:nth-child(2) > div:nth-child(1) > font").get(0);
           //輸出目前使用者賬戶積分
            System.out.println(element.text());


        }
    }

}