Java 无头浏览器 暴破网站登录

登录获取token 可以说是爬虫里面最难的点,其他的只要能在浏览器上显示的都可以用http工具获取。只要稍微有点安全意识的网站登录都比较复杂,JS混淆甚至可能密码加密等等操作。

工具准备

1、selenium 依赖添加。

    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

2、谷歌浏览器驱动。驱动下载-》http://npm.taobao.org/mirrors/chromedrive 或者
https://chromedriver.storage.googleapis.com/index.html?path=75.0.3770.140/

3、本地电脑安装谷歌浏览器,(其他浏览器也可以,需要下载对应其他的驱动程序)

编码爆破获取token

package cn.harsons.crawling.crawling.reptile;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyabin
 * @date 2020/3/20 0020
 */
@Slf4j
@Component
public class TokenHander {

    @Autowired
    private Config config;

    public String token() throws Exception {
        //设置本地chrome driver地址 这里是你下载的谷歌浏览器 驱动路径。
        System.setProperty ("webdriver.chrome.driver", config.getDrivePath ());
        //创建无Chrome无头参数
        ChromeOptions chromeOptions = new ChromeOptions ();
        chromeOptions.addArguments ("-headless");
        //创建Drive实例
        WebDriver driver = new ChromeDriver (chromeOptions);
        // 打开网站
        driver.get (config.getLoginUrl ());
        String currentUrl = driver.getCurrentUrl ();
        log.info ("当前连接" + currentUrl);
        // 获取浏览器页面上的节点  类似 document.getElementById()
        //拿到用户名 和密码的输入框句柄
        WebElement userName = driver.findElement (By.id ("userName"));
        WebElement password = driver.findElement (By.id ("password"));
        //向用户名和密码输入框中写入值
        userName.sendKeys (config.getUserName ());
        password.sendKeys (config.getPassword ());
        // 拿到登录按钮的句柄
        WebElement login_button = driver.findElement (By.className ("btn-submit"));
        // 模拟点击登录
        login_button.click ();
        currentUrl = driver.getCurrentUrl ();
        log.info ("当前连接" + currentUrl);
        // 拿到cookie 信息
        Set<Cookie> cookies = driver.manage ().getCookies ();
        return cookies.stream ().map (entity -> entity.getName () + "=" + entity.getValue ()).collect (Collectors.joining (";"));
    }


}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录1.简介2.安装 Selenium3.安装驱动3.1 Chrome3.2 Firefox3.3 其它浏览器驱动...
    所謂向日葵族阅读 2,284评论 0 1
  • 现状 好久好久没有更新博客了,应该有一个月了吧,这段时间内,好忙,公司的业务在上涨期,但是却把下面的一个小朋友砍掉...
    jb2阅读 1,326评论 0 2
  • 上网原理 1、爬虫概念 爬虫是什麽? 蜘蛛,蛆,代码中,就是写了一段代码,代码的功能从互联网中提取数据 互联网: ...
    riverstation阅读 8,273评论 1 2
  • 2018年8月 31日 姓名:潘红军 公司 :扬州市方圆建筑工程有限公司 【日精进打卡第222天】 南京第349...
    5119a64ee3ab阅读 79评论 0 0
  • 每个女人都在寻找一个爷们,最后发现,最爷们的原来是自己。不要依靠别人,要学会利用别人。
    A分享阅读 410评论 0 2