Java爬虫——模拟登录知乎

时间:2023-03-08 15:56:27
Java爬虫——模拟登录知乎

Java爬虫——模拟登录知乎

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

Java爬虫——模拟登录知乎

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

发送的表单是

_xsrf: 
password: 密码 无需加密
captcha: 验证码 无需验证码时为不用此项 ,需要验证码时为验证码图片倒立字体坐标
captcha_type:cn
phone_num: 账号
 package 知乎模拟登录;

 import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import java.util.ArrayList;
import java.util.List; public class GetProblem {
public static void main(String[] args) throws Exception {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault() ;
HttpPost httpPost = new HttpPost("https://www.zhihu.com/login/phone_num") ;
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("_xsrf", "66653239623962342d396237632d346233332d396331362d333434386438326438616139"));
nvps.add(new BasicNameValuePair("password", "33665511886622"));
nvps.add(new BasicNameValuePair("captcha_type", "cn"));
nvps.add(new BasicNameValuePair("phone_num", "15890956765")); httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost) ;
HttpEntity entity = closeableHttpResponse.getEntity() ;
String s = EntityUtils.toString(entity);
System.out.println(s);
}
}

可能出现的情况

请求返回体为:

    {

  "r": 1,
  "errcode": 1991829,
  "data": {"captcha":"\u9a8c\u8bc1\u7801\u9519\u8bef"},
  "msg": "\u9a8c\u8bc1\u7801\u9519\u8bef"
 }

"errcode": 1991829