selenium模拟第三方qq快捷登录,jsoup解析页面

1、引入依赖

   compile group: 'org.jsoup', name: 'jsoup', version: '1.11.3'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.5'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium.client-drivers/selenium-java-client-driver
    testCompile group: 'org.seleniumhq.selenium.client-drivers', name: 'selenium-java-client-driver', version: '1.0.2'

2、配置浏览器驱动

下载地址:https://www.npmjs.com/package/selenium-webdriver

selenium模拟第三方qq快捷登录,jsoup解析页面_第1张图片

 

windows下配置驱动环境变量

selenium模拟第三方qq快捷登录,jsoup解析页面_第2张图片

 

3、使用selenium 进行qq快捷登录

/**
 * Copyright (C), 2018-2018
 * FileName: Test2
 * Author:   lei.ma
 * Date:     2018/11/13 9:08
 * Description:
 */
package com;

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.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

/**
 * 〈一句话功能简述〉
* 〈〉 * * @author: lei.ma * @create: 2018/11/13 9:08 * @since 1.0.0 */ public class Test2 { public static void main(String[] args) throws InterruptedException { ChromeOptions chromeOptions = new ChromeOptions(); //隐式运行 // chromeOptions.addArguments("--headless"); // chromeOptions.addArguments("--no-sandbox"); WebDriver driver = new ChromeDriver(chromeOptions); driver.get("http://www.58pic.com/"); WebElement login = ((ChromeDriver) driver).findElementByClassName("sky-eyes-login"); login.click(); WebElement elementById = ((ChromeDriver) driver).findElementById("statis-qq"); elementById.click(); //切换到登录iframe WebDriver ptlogin_iframe = driver.switchTo().frame("ptlogin_iframe"); WebDriverWait wait = new WebDriverWait(ptlogin_iframe, 10); // try { // wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("switcher_plogin"))); //点击账号密码登录按钮 WebElement element = ((ChromeDriver) ptlogin_iframe).findElementById("switcher_plogin"); element.click(); //获取到用户名输入框,并输入密码 WebElement uBox = ptlogin_iframe.findElement(By.id("u")); uBox.sendKeys("12"); //获取到密码输入框,并输入密码 WebElement pBox = ptlogin_iframe.findElement(By.id("p")); pBox.sendKeys("1212"); //点击登录 WebElement searchButton = ptlogin_iframe.findElement(By.id("login_button")); searchButton.click(); //获取cookie driver.get("http://www.58pic.com/"); String qt_visitor_id = driver.manage().getCookieNamed("qt_visitor_id").getValue(); System.out.println("qt_visitor_id:"+qt_visitor_id); while(true){ ((ChromeDriver) driver).executeScript("location.reload()"); System.out.println("刷新页面"); TimeUnit.SECONDS.sleep(10); } // } catch (Exception e) { // System.out.println("没找到登录按钮"); // e.printStackTrace(); // } } }

4、jsonp获取下载地址

/**
 * Copyright (C), 2018-2018
 * FileName: Test
 * Author:   lei.ma
 * Date:     2018/11/12 13:44
 * Description: 下载
 */
package com;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Map;

/**
 * 〈一句话功能简述〉
* 〈下载〉 * * @author: lei.ma * @create: 2018/11/12 13:44 * @since 1.0.0 */ public class Test { private static boolean checklogin(String authId) throws IOException { Connection connect = Jsoup.connect("http://www.58pic.com").cookie("auth_id", authId); Document document = connect.get(); Element loginBtn = document.getElementById("loginBtn"); if (null == loginBtn) { return true; } else { System.out.println("未登录"); return false; } } public static void main(String[] args) throws IOException { String authId = "%22415971%7C%5Cu7c73%5Cu7eb3%5Cu55b7%5Cu753b%5Cu90e8%7C1542606777%7C3a9ea6560e6fb8e17c5a3e5d459fb2d6%22"; String url = "http://www.58pic.com/newpic/32435780.html"; boolean checklogin = checklogin(authId); if (checklogin) { download(authId, url); } } private static void download(String authId, String url) throws IOException { long currentTimeMillis = System.currentTimeMillis(); Connection connect = Jsoup.connect(url); Document document = connect.get(); Connection.Response response = connect.response(); Map cookies = response.cookies(); String cookie = response.cookie("auth_id"); System.out.println("返回的cookie:" + cookie); Elements elementsByClass = document.getElementsByClass("detailBtn-down"); if (null != elementsByClass && elementsByClass.size() > 0) { String href = elementsByClass.first().attr("data-href"); Connection downloadConnect = Jsoup.connect(href).cookie("auth_id", authId); Document document1 = downloadConnect.get(); String title = document1.title(); if(!StringUtil.isBlank(title)&&title.contains("受限")){ System.out.println("下载受限"); return; } Element first = document1.getElementsByClass("down-waiting inform qidong").first(); if (null != first) { String downloadHref = first.child(0).child(0).attr("href"); System.out.println("下载地址是:" + downloadHref); } } System.out.println(System.currentTimeMillis() - currentTimeMillis); } }

 

你可能感兴趣的:(java)