天天看點

Java爬蟲——模拟登入知乎

Java爬蟲——模拟登入知乎

登入界面,首先随意輸入一個賬号,登入檢視發送表單的請求

Java爬蟲——模拟登入知乎

 可以發現請求是Post : https://www.zhihu.com/login/phone_num

發送的表單是

_xsrf: 
password: 密碼  無需加密
captcha: 驗證碼  無需驗證碼時為不用此項 ,需要驗證碼時為驗證碼圖檔倒立字型坐标
captcha_type:cn
phone_num: 賬号

      
1 package 知乎模拟登入;
 2 
 3 import org.apache.http.HttpEntity;
 4 import org.apache.http.NameValuePair;
 5 import org.apache.http.client.entity.UrlEncodedFormEntity;
 6 import org.apache.http.client.methods.CloseableHttpResponse;
 7 import org.apache.http.client.methods.HttpPost;
 8 import org.apache.http.impl.client.CloseableHttpClient;
 9 import org.apache.http.impl.client.HttpClients;
10 import org.apache.http.message.BasicNameValuePair;
11 import org.apache.http.protocol.HTTP;
12 import org.apache.http.util.EntityUtils;
13 
14 import java.util.ArrayList;
15 import java.util.List;
16 
17 public class GetProblem {
18     public static void main(String[] args) throws Exception {
19         CloseableHttpClient closeableHttpClient = HttpClients.createDefault() ;
20         HttpPost httpPost = new HttpPost("https://www.zhihu.com/login/phone_num") ;
21         List<NameValuePair> nvps = new ArrayList<NameValuePair>();
22         nvps.add(new BasicNameValuePair("_xsrf", "66653239623962342d396237632d346233332d396331362d333434386438326438616139"));
23         nvps.add(new BasicNameValuePair("password", "1325200471"));
24         nvps.add(new BasicNameValuePair("captcha_type", "cn"));
25         nvps.add(new BasicNameValuePair("phone_num", "15890956765"));
26 
27         httpPost.setEntity(new UrlEncodedFormEntity(nvps));
28         CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost) ;
29         HttpEntity entity = closeableHttpResponse.getEntity() ;
30         String s = EntityUtils.toString(entity);
31         System.out.println(s);
32     }
33 }       

   可能出現的情況

   請求傳回體為:

    {

  "r": 1,

  "errcode": 1991829,

  "data": {"captcha":"\u9a8c\u8bc1\u7801\u9519\u8bef"},

  "msg": "\u9a8c\u8bc1\u7801\u9519\u8bef"

 }

"errcode": 1991829