Java爬虫-模拟登录知乎

 

 

登录界面,首先随意输入一个账号,登录查看发送表单的请求

 

 

 

 可以发现请求是Post : https://www.zhihu.com/login/phone_num

 

发送的表单是

 

 
  1. _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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值