很多年前写着玩的代码, 也没啥用了, 就留这里吧.
package clipconverter;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
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.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import uitl.Env;
public class Clipconverter {
private static Logger log = Logger.getLogger ( Clipconverter.class );
public static void main ( String [ ] args ) {
String url = getD ( "url" , "https://ytb.com/watch?v=vhAN73OH3h0" ); //要下载的视频地址
if ( StringUtils.isBlank ( url ) ) throw new RuntimeException ( "" );
DesiredCapabilities capabilities = DesiredCapabilities.chrome ( );
ChromeOptions optionsChrome = new ChromeOptions ( );
// ////////////
System.setProperty ( "webdriver.chrome.driver" , getD ( "driver" , "C:/chrome/chromedriver79.exe" ) ); // chromedriver 浏览器驱动
System.setProperty ( "webdriver.chrome.logfile" , getD ( "logfile" , "c:/webdriver.chrome.log" ) ); // chrome log
String options = getD ( "options" , "" );
if ( StringUtils.isNotBlank ( options ) ) {
String [ ] optarray = options.split ( "," );
optionsChrome.addArguments ( optarray );
}
optionsChrome.addArguments ( "--incognito" ); // 隐私模式
optionsChrome.addArguments ( "--disable-plugins" ); // 不加载插件
optionsChrome.addArguments ( "--start-maximized" ); // 窗口最大化
optionsChrome.addArguments ( "--dns-prefetch-disable" );
// ////////////
optionsChrome.setBinary ( new File ( getD ( "chrome" , "C:/chrome/Chrome79/chrome.exe" ) ) ); // chrome浏览器路径
capabilities.setCapability ( "ignoreProtectedModeSettings" , true );
capabilities.setBrowserName ( getD ( "browserName" , "Clipconverter" ) );
capabilities.setCapability ( ChromeOptions.CAPABILITY , optionsChrome );
capabilities.setPlatform ( Platform.getCurrent ( ) );
// ///////////////
WebDriver driver = null;
driver = new ChromeDriver ( capabilities );
driver.manage ( ).timeouts ( ).pageLoadTimeout ( getD ( "pageLoadTimeout" , 10 ) , TimeUnit.SECONDS ); // page load timeout
driver.manage ( ).timeouts ( ).setScriptTimeout ( getD ( "scriptTimeout" , 10 ) , TimeUnit.SECONDS ); // js exec timeout
driver.manage ( ).timeouts ( ).implicitlyWait ( getD ( "implicitlyWait" , 10 ) , TimeUnit.SECONDS ); // ,告诉WebDriver查询Dom一定时间
try {
driver.get ( getD ( "host" , "https://www.clipconverter.cc" ) ); //打开页面
} catch ( Exception e ) {
// e.printStackTrace ( );
}
try {
// / input url
WebElement mediaurl = driver.findElement ( By.xpath ( "//input[@id='mediaurl']" ) );
( (JavascriptExecutor) driver ).executeScript ( "arguments[0].setAttribute('value', arguments[1])" , mediaurl , url ); // 填写input
// / click submiturl
WebElement submiturl = driver.findElement ( By.xpath ( "//input[@id='submiturl']" ) );
( (JavascriptExecutor) driver ).executeScript ( "arguments[0].click();" , submiturl ); // 点击提交按钮
} catch ( Exception e ) {
e.printStackTrace ( );
quit ( driver , -1 );
throw new RuntimeException ( "页面加载错误,退出" , e );
}
int t = 1; // /每1MB等1秒
try {
// / click selecturl
WebElement selecturl = driver.findElement ( By.xpath ( "//div[@id='selecturl']/input[@id='0']" ) );
( (JavascriptExecutor) driver ).executeScript ( "arguments[0].click();" , selecturl );
// / get size
String size = driver.findElement ( By.xpath ( "//div[@id='selecturl']/label[@for='0']/strong[2]" ) ).getText ( );
// ( (JavascriptExecutor) driver ).executeScript ( "alert('size:" + size + "');" );
if ( size.endsWith ( "MB" ) ) {
String int_size = size.replace ( "MB" , "" ).trim ( );
t = 1 + Integer.valueOf ( int_size ) / 10;
t = 10 * (int) Math.ceil ( t );
log.info ( size + ", wait for [" + t + "] seconds" );
}
// / click MOV radio
WebElement radio_MOV = driver.findElement ( By.xpath ( "//div[@id='radio']/input[@value='MOV']" ) );
( (JavascriptExecutor) driver ).executeScript ( "arguments[0].click();" , radio_MOV );
sleep ( 1 );
// / click submitconvert
WebElement submitconvert = driver.findElement ( By.xpath ( "//div[@id='submitconvert']/input" ) );
( (JavascriptExecutor) driver ).executeScript ( "arguments[0].click();" , submitconvert );
} catch ( Exception e ) {
e.printStackTrace ( );
}
// / 等待页面加载完, ... Conversion successfully completed!
new WebDriverWait ( driver , t ).until ( new ExpectedCondition < Boolean > ( ) {
public Boolean apply ( WebDriver d ) {
return d.getPageSource ( ).indexOf ( "Conversion successfully completed!" ) > 0;
}
} );
// / click downloadbutton
final WebElement downloadbutton = driver.findElement ( By.xpath ( "//a[@id='downloadbutton']" ) );
( (JavascriptExecutor) driver ).executeScript ( "arguments[0].click();" , downloadbutton ); // 点击下载按钮(浏览器会自动下载到默认目录)
sleep ( 2 );
}
// 浏览器退出
private static void quit ( WebDriver driver , int i ) {
try {
driver.quit ( );
} catch ( Exception e1 ) {
e1.printStackTrace ( );
}
}
private static void sleep ( int i ) {
try {
Double d = i * 1000D * getD ( "sleep_unit_by_second" , 1 );
TimeUnit.MILLISECONDS.sleep ( d.longValue ( ) );
System.out.println ( "sleep " + d );
} catch ( Exception e ) {
try {
TimeUnit.SECONDS.sleep ( 2 );
} catch ( InterruptedException e1 ) {
}
}
}
private static int getD ( String key , int defaults ) {
return Env.getD ( key , defaults );
}
private static String getD ( String key , String defaults ) {
return Env.getD ( key , defaults );
}
}
package selenium ;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import uitl.Env;
import uitl.Kill;
public class Fenfenmei {
private static Logger log = Logger.getLogger ( Fenfenmei.class );
public static void main ( String [ ] args ) throws Exception {
String local_html = getD ( "html" , "c:/aaa.html" );
String pac = getD ( "pac" , "http://127.0.0.1:23480/pac/opl.pac" ); //本地pac代理(可以自定义代理网址)
if ( "true".equals ( getD ( "clean" , "false" ) ) ) {
Kill.kill ( "chromedriver79.exe" );
Kill.kill ( "chrome.exe" );
}
// 设置禁止加载项
Map < String , Object > prefs = new HashMap < String , Object > ( );
if ( "false".equals ( getD ( "javascript" , "true" ).trim ( ) ) ) {
// 禁止加载js
prefs.put ( "profile.default_content_settings.javascript" , 2 ); // 2就是代表禁止加载的意思
}
if ( "false".equals ( getD ( "css" , "true" ).trim ( ) ) ) {
// 禁止加载css
prefs.put ( "profile.default_content_settings.css" , 2 ); // 2就是代表禁止加载的意思
}
if ( "false".equals ( getD ( "images" , "true" ).trim ( ) ) ) {
// 禁止加载图片
prefs.put ( "profile.default_content_settings.images" , 2 ); // 2就是代表禁止加载的意思
}
DesiredCapabilities capabilities = DesiredCapabilities.chrome ( );
ChromeOptions optionsChrome = new ChromeOptions ( );
// ////////////
System.setProperty ( "webdriver.chrome.driver" , getD ( "driver" , "C:/chrome/chromedriver79.exe" ) );
System.setProperty ( "webdriver.chrome.logfile" , getD ( "logfile" , "c:/webdriver.chrome.log" ) );
String options = getD ( "options" , "" );
if ( StringUtils.isNotBlank ( options ) ) {
String [ ] optarray = options.split ( "," );
optionsChrome.addArguments ( optarray );
optionsChrome.addArguments ( "--incognito" );
optionsChrome.addArguments ( "--disable-plugins" );
optionsChrome.addArguments ( "--start-maximized" );
optionsChrome.addArguments ( "--dns-prefetch-disable" );
// optionsChrome.addArguments("headless"); ///加载浏览器的静默模式,使浏览器在后台运行
}
// ////////////
optionsChrome.setBinary ( new File ( getD ( "chrome" , "C:/chrome/Chrome79/chrome.exe" ) ) );
capabilities.setCapability ( "ignoreProtectedModeSettings" , true );
if ( "true".equals ( getD ( "proxy" , "true" ).trim ( ) ) ) {
capabilities.setCapability ( "proxy" , new Proxy ( ).setProxyAutoconfigUrl ( pac ) ); //设置浏览器pac代理
}
optionsChrome.setExperimentalOption ( "prefs" , prefs );
capabilities.setBrowserName ( getD ( "browserName" , "Fenfenmei" ) );
capabilities.setCapability ( ChromeOptions.CAPABILITY , optionsChrome );
capabilities.setPlatform ( Platform.getCurrent ( ) );
// ///////////////
WebDriver driver = null;
driver = new ChromeDriver ( capabilities );
driver.manage ( ).timeouts ( ).pageLoadTimeout ( getD ( "pageLoadTimeout" , 60 ) , TimeUnit.SECONDS ); // page load timeout
driver.manage ( ).timeouts ( ).setScriptTimeout ( getD ( "pageLoadTimeout" , 30 ) , TimeUnit.SECONDS ); // js exec timeout
driver.manage ( ).timeouts ( ).implicitlyWait ( getD ( "pageLoadTimeout" , 30 ) , TimeUnit.SECONDS ); // ,告诉WebDriver查询Dom一定时间
if ( !_pac_ok ( pac ) ) throw new RuntimeException ( "代理服务器不可用,请检查]" );
try {
driver.get ( getD ( "host" , "https://www.ytb.com/channel/UCeFL-kNY6QPV6tu0v6vGjEA/videos" ) ); // 打开主页面
} catch ( Exception e ) {
e.printStackTrace ( );
}
try { // 滚动截图, 现在可用国产浏览器的截图命令更好一些.
int q = getD ( "index" , 10000 );
FileUtils.copyFile ( ( (TakesScreenshot) driver ).getScreenshotAs ( OutputType.FILE ) ,//
new File ( new File ( local_html ).getParent ( ) + "/" + new File ( local_html ).getName ( ) + "." + q * 1 + ".png" ) );
( (JavascriptExecutor) driver ).executeScript ( "var q=document.documentElement.scrollTop=" + q * 1 );
sleep ( 2 );
FileUtils.copyFile ( ( (TakesScreenshot) driver ).getScreenshotAs ( OutputType.FILE ) ,//
new File ( new File ( local_html ).getParent ( ) + "/" + new File ( local_html ).getName ( ) + "." + q * 2 + ".png" ) );
( (JavascriptExecutor) driver ).executeScript ( "var q=document.documentElement.scrollTop=" + q * 2 );
sleep ( 2 );
FileUtils.copyFile ( ( (TakesScreenshot) driver ).getScreenshotAs ( OutputType.FILE ) , //
new File ( new File ( local_html ).getParent ( ) + "/" + new File ( local_html ).getName ( ) + "." + q * 3 + ".png" ) );
( (JavascriptExecutor) driver ).executeScript ( "var q=document.documentElement.scrollTop=" + q * 3 );
sleep ( 2 );
FileUtils.copyFile ( ( (TakesScreenshot) driver ).getScreenshotAs ( OutputType.FILE ) , //
new File ( new File ( local_html ).getParent ( ) + "/" + new File ( local_html ).getName ( ) + "." + q * 4 + ".png" ) );
( (JavascriptExecutor) driver ).executeScript ( "var q=document.documentElement.scrollTop=" + q * 4 );
sleep ( 2 );
} catch ( Exception e ) {
e.printStackTrace ( );
}
savePage ( new File ( local_html ) , driver.getPageSource ( ) );
File png = ( (TakesScreenshot) driver ).getScreenshotAs ( OutputType.FILE );
String local_png = new File ( local_html ).getParent ( ) + "/" + new File ( local_html ).getName ( ) + ".png";
FileUtils.copyFile ( png , new File ( local_png ) );
// driver.navigate ( ).refresh ( );
sleep ( 2 );
// driver.quit ( );
// WebElement _div_continue = driver.findElement ( By.xpath ( "//div[@class='gray-button browse-site-button touchable']" ) );
// ( (JavascriptExecutor) driver ).executeScript ( "arguments[0].click();" , _div_continue );
// /赋值
// WebElement _input_Login_email = driver.findElement ( By.xpath ( "//input[@class='login-email']" ) );
// WebElement _input_Login_password = driver.findElement ( By.xpath ( "//input[@class='login-password']" ) );
// ( (JavascriptExecutor) driver ).executeScript ( "arguments[0].setAttribute('value', arguments[1])" , _input_Login_email , "email" );
// ( (JavascriptExecutor) driver ).executeScript ( "arguments[0].setAttribute('value', arguments[1])" , _input_Login_password , "password" );
// / 等待,...
// final WebElement _div_buy_redo = driver.findElement ( By.xpath ( "//td[@class='banner-table-cell banner-button-cell']/div[@class='buy-button orange-button touchable default-hide']" ) );
// ( new WebDriverWait ( driver , 10 ) ).until ( new ExpectedCondition < Boolean > ( ) {
// public Boolean apply ( WebDriver d ) {
// return _div_buy_redo.isDisplayed ( );
// }
// } );
}
private static void savePage ( File f , String pageSource ) throws IOException {
FileUtils.write ( f , pageSource );
}
private static void sleep ( int i ) {
try {
Double d = i * 1000D * getD ( "sleep_unit_by_second" , 1 );
TimeUnit.MILLISECONDS.sleep ( d.longValue ( ) );
System.out.println ( "sleep " + d );
} catch ( Exception e ) {
try {
TimeUnit.SECONDS.sleep ( 2 );
} catch ( InterruptedException e1 ) {
}
}
}
private static boolean _pac_ok ( String pac ) throws IOException {
log.info ( "pac代理: " + pac );
String html = Jsoup.connect ( pac ).timeout ( 2000 ).ignoreContentType ( true ).get ( ).text ( );
if ( StringUtils.isBlank ( html ) ) throw new IOException ( "代理服务器不可用,请检查]" );
// log.info ( html );
return true;
}
private static String getD ( String key , String defaults ) {
return Env.getD ( key , defaults );
}
private static int getD ( String key , int defaults ) {
return Env.getD ( key , defaults );
}
}
package uitl;
// 获取jvm参数的方法,现在可以直接用hutool更好一些
public class Env {
public static String getD ( String key , String defaults ) {
String val = System.getenv ( key );
return null == val ? defaults : val.trim ( );
}
public static int getD ( String key , int defaults ) {
String val = getD ( key , "" + defaults );
return Integer.valueOf ( val.trim ( ) );
}
}
package uitl;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.openqa.selenium.os.WindowsUtils;
//按名称杀进程
public class Kill {
private static Logger log = Logger.getLogger ( Kill.class );
public static void main ( String [ ] args ) throws IOException {
for ( String a : args ) {
kill ( a );
}
// kill ( "chromedriver79.exe" );
// kill ( "chrome.exe" );
// kill ( "nginx.exe" );
}
public static void kill ( String _name ) {
try {
System.out.println ( "kill " + _name );
log.info ( "kill " + _name );
WindowsUtils.killByName ( _name ); //按名称杀进程
} catch ( Exception e ) {
e.printStackTrace ( );
}
}
}