java+selenium-java 实现qq自动登录功能并获取cookie
下面是网易云音乐的qq自动功能演示,cookie信息在控制台中会打印出来。---->下一步就是利用这个cookie实现网站资源的爬取。
下面是qq音乐的自动登录
CSDN的自动登录
源代码如下
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;public class NeteaseCloudMusicDemo {public static void main(String[] args) {//设置系统环境变量
// System.setProperty("webdriver.chrome.driver", "D:/Program Files (x86)/chromedriver/chromedriver.exe");WebDriver driver = null;try {ChromeOptions options = new ChromeOptions();options.addArguments("--headless"); //无浏览器模式driver = new ChromeDriver(options);//实例化// qq音乐// driver.get("https://graph.qq.com/oauth2.0/authorize?response_type=code&state=state&client_id=100497308&redirect_uri=https://y.qq.com/portal/wx_redirect.html?login_type=1%26surl=https%3A%2F%2Fy.qq.com%2Fportal%2Fradio.html%23stat%3Dy_new.top.pop.logout%26stat%3Dy_new.top.pop.logout%26stat%3Dy_new.top.pop.logout%26stat%3Dy_new.top.pop.logout%26stat%3Dy_new.top.pop.logout%26use_customer_cb=0");// csdn// driver.get("https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=100270989&response_type=code&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Flogin%3FpcAuthType%3Dqq%26state%3Dtest");// 网易云音乐driver.get("https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=100495085&response_type=code&redirect_uri=https://music.163.com/back/qq&forcelogin=true&state=LPByMqvmCH&checkToken=9ca17ae2e6ffcda170e2e6eea5f67082abbcabe9508f9e8aa2d84e879a9aaeaa5cf38b88a3f042f589beacf92af0feaec3b92af8a8fa82f721f2ba9eb0fb5b969a8ea6c84aa1ab848cc5419188add9c4629a8bee9e");// 其它的网站一般直接复制登录页面的url替代这个get即可// driver.get("其它网站的qq登录");final WebDriver ptlogin_iframe = driver.switchTo().frame("ptlogin_iframe");ptlogin_iframe.findElement(By.id("switcher_plogin")).click();final WebElement u = ptlogin_iframe.findElement(By.className("inputstyle"));u.click();final char[] chars = "输入qq账号".toCharArray();for (int i = 0; i < chars.length; i++) {try {TimeUnit.MICROSECONDS.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}u.sendKeys("" + chars[i]);}final WebElement login_button = ptlogin_iframe.findElement(By.id("login_button"));
// final WebElement qq_account = ptlogin_iframe.findElement(By.className("input_tips"));final WebElement p = ptlogin_iframe.findElement(By.id("p"));final char[] chars1 = "输入qq密码".toCharArray();for (int i = 0; i < chars1.length; i++) {p.sendKeys("" + chars1[i]);}p.sendKeys("\n");try {login_button.click();TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {System.out.println("<><><>");}System.out.println(driver.getCurrentUrl());//获得cookieSet<Cookie> coo = driver.manage().getCookies();//打印cookieSystem.out.println(coo);try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}System.out.println();} catch (Exception e) {System.out.println("抛异常了");e.printStackTrace();} finally {//使用完毕,关闭webDriverif (driver != null) {driver.quit();}}}
}
开源项目地址:https://github.com/1015770492/autoLoginQQWithAnyWeb
说明:在上面给出的网易云、qq音乐、csdn的自动登录的get方法中的url这里讲解一下:
需要四个参数:
response_type=code 这个值是固定的
client_id 指的是appid
redirect_uri 指的是网站的回调域
state 给一个值就行,一般不做要求。
最简单的方式就是
要想执行这个程序,得先搭建好环境
- chrome浏览器
- 驱动chromedriver 这个就相当于客户端,而chrome则是服务端,通过java调用api让chrome按照代码得逻辑执行对应得操作
- 项目中引入selenium依赖
原理:QQ互联的文档
qq音乐只是做的前端拦截,发的请求带有校验参数,我直接去掉无关参数,就直接登录成功。要知道很多第三方应用都是没有校验的,或者校验是不一致的,因此可以实现任意网站的自动登录,但需要通过抓包分析,或者F12找到对应的请求,然后得到client_id以及redirect_uri就可以直接登录成功!!!
觉得写的不错的,欢迎一键三连。