selenium-java 实现QQ音乐自动登录获取cookie数据,爬取qq音乐会员资源和网易云音乐会员资源。实现任意网站的会员资源爬取

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());
            //获得cookie
            Set<Cookie> coo = driver.manage().getCookies();
            //打印cookie
            System.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 {
            //使用完毕,关闭webDriver
            if (driver != null) {
                driver.quit();
            }
        }
    }
}

开源项目地址:https://github.com/1015770492/autoLoginQQWithAnyWeb

说明:在上面给出的网易云、qq音乐、csdn的自动登录的get方法中的url这里讲解一下:

需要四个参数:
response_type=code 这个值是固定的
client_id 指的是appid
redirect_uri 指的是网站的回调域
state 给一个值就行,一般不做要求。

最简单的方式就是
在这里插入图片描述


要想执行这个程序,得先搭建好环境

  1. chrome浏览器
  2. 驱动chromedriver 这个就相当于客户端,而chrome则是服务端,通过java调用api让chrome按照代码得逻辑执行对应得操作
  3. 项目中引入selenium依赖

原理:QQ互联的文档

qq音乐只是做的前端拦截,发的请求带有校验参数,我直接去掉无关参数,就直接登录成功。要知道很多第三方应用都是没有校验的,或者校验是不一致的,因此可以实现任意网站的自动登录,但需要通过抓包分析,或者F12找到对应的请求,然后得到client_id以及redirect_uri就可以直接登录成功!!!

觉得写的不错的,欢迎一键三连。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗水人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值