java爬虫 模拟登录_java 基于Jsoup 对领英完成模拟登陆(Java爬虫)

public String login() {

// TODO Auto-generated method stub

String pwd = "XXXXXXXXXXXXXXX";//密码

String account = "XXXXXXXXXXXXXXXXXXXX";//账号

String nameId="";

String csrf_token="";

Map cookie = new HashMap();

Map cookies = new HashMap();

try {

String url="https://www.linkedin.com/login?trk=guest_homepage-basic_nav-header-signin";

Connection connect = Jsoup.connect(url);

connect.header("authority","www.linkedin.com");

connect.header("cache-control","max-age=0,no-cache");

connect.header("user-agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");

connect.header("accept","text/html,appli cation/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");

connect.header("referer","https://www.baidu.com/s?ie=·-8&wd=%E9%A2%86%E8%8B%B1");

connect.header("accept-encoding","gzip, deflate, br");

connect.header("accept-language","en-US;q=0.8,en;q=0.7");

Connection.Response response = connect.ignoreContentType(true)

.execute();

Document parse = response.parse();

String loginCsrfParam = parse.select("input[name=loginCsrfParam]").get(0).attr("value");

cookie = response.cookies();

System.out.println(cookie);

for (String s : cookie.keySet()) {

if(s.equalsIgnoreCase("JSESSIONID")){

csrf_token=cookie.get(s);

System.out.println(csrf_token);

break;

}

}

Map datas = new HashMap();

datas.put("session_key", account);

datas.put("session_password",pwd );

datas.put("isJsEnabled", "false");

datas.put("loginCsrfParam",loginCsrfParam);

datas.put("fp_data", "default");

datas.put("undefined", "");

url="https://www.linkedin.com/uas/login-submit?loginSubmitSource=GUEST_HOME";

Connection connection2 = Jsoup.connect(url);

connection2.header("authority","www.linkedin.com");

connection2.header("cache-control","max-age=0,no-cache");

connection2.header("origin","https://www.linkedin.com");

connection2.header("upgrade-insecure-requests","1");

connection2.header("content-type","application/x-www-form-urlencoded");

connection2.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");

connection2.header("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");

connection2.header("referer","https://www.linkedin.com/");

connection2.header("accept-encoding","gzip, deflate, br");

connection2.header("accept-language","en-US;q=0.8,en;q=0.7");

connection2.cookies(cookie);

Connection.Response execute = connection2

.data(datas)

.cookies(cookie)

.method(Method.POST)

.execute();

URL referer = execute.url();

Map executeCookie = execute.cookies();

Document parse1 = execute.parse();

String result=parse1.toString();

if (result.contains("Sign-In Verification") || result.contains("verification code")) {

cookie.putAll(executeCookie);

cookies =cookie;

}else{

cookies =executeCookie;

}

nameId = RegexUtil.getValue("\"publicIdentifier\":\"(.*?)\"", result, 1).trim();

System.out.println("登陆成功");

return "success";

} catch (Exception e) {

System.out.println("登陆异常");

return "error";

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值