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", "33665511886622"));
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

 

posted @ 2017-12-11 13:54  AntzUhl  阅读(3800)  评论(7编辑  收藏  举报