• 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":"u9a8cu8bc1u7801u9519u8bef"},
      "msg": "u9a8cu8bc1u7801u9519u8bef"
     }

    "errcode": 1991829

     

  • 相关阅读:
    深入理解TCP协议及其源代码
    Socket与系统调用深度分析
    构建调试Linux内核网络代码的环境MenuOS系统
    创新产品的需求分析:未来的图书会是什么样子?
    socket通信的原理与实践
    案例分析:设计模式与代码的结构特性
    网络相关的命令工具研究报告:ssh
    业务领域建模Domain Modeling
    ubuntu 设置静态ip,但显示scope global secondary ens33
    解决Python查询Mysql中文乱码问题
  • 原文地址:https://www.cnblogs.com/LexMoon/p/JavaZh.html
Copyright © 2020-2023  润新知