Java + selenium实现刷博客访问量
程序员文章站
2022-04-26 17:06:22
...
丑话: 毕竟丑话要说在前边,刷访问量CSDN后台还是很容易检测到的,毕竟同一个IP一直重新访问,检测到后怎么处理我也不清楚,猜测是封号或者清除该篇博客访问量和收益,所以一定慎用,慎用,慎用,而且因为CSDN的机制(这个后边会说到),这个刷访问量的效率也实在不怎么高。
1、使用账号访问博客代码
注释还是比较清楚
如要测试代码,需要适配谷歌浏览器安装路径、driver路径、账号,密码和博客名称
package com.csdnTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
public static WebDriver driver = null;
public static int count = 1;
public static int countbig = 1;
public static void main(String[] args) {
// TODO Auto-generated method stub
while (true){
System.out.println("开始web自动化第" + countbig + "次循环!");
count=1;
driver=null;
countbig++;
waitTime(30);
try {
test();
close();
} catch (Exception e) {
System.out.println("捕获到异常:::" + e);
waitTime(20);
close();
}
}
}
//测试主体代码
public static void test(){
//启动浏览器的参数(所有都是可选参数,即可有可无)
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized"); //启动浏览器最大化
options.addArguments("--allow-silent-push");
options.addArguments("--incognito");
options.addArguments("--v8-cache-options");
driver = new ChromeDriver(options);
//获取dirver本地地址,用驱动打开浏览器
String dirverpath = System.getProperty("user.dir");
System.out.println("当前项目" + dirverpath);
System.setProperty(dirverpath +"\\src\\Drivers\\chromedriver-86.exe", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
waitTime(3);
//访问参数
String user = "csdn账号";
String password = "密码";
String blogName = "博客名称";
driver.get("https://passport.csdn.net/login");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
waitTime(1);
//所有页面操作
driver.findElement(By.xpath("//*[@id='app']//ul//a[text()='账号密码登录']")).click();
driver.findElement(By.xpath("//*[@id=\"all\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password-number\"]")).sendKeys(password);
waitTime(3);
driver.findElement(By.xpath("//*[@id=\"app\"]//div/button[text()='登录']")).click();
waitTime(5);
driver.findElement(By.xpath("//*[@id=\"csdn-toolbar\"]/div/div/div[3]/div[3]/a")).click();
waitTime(1);
driver.findElement(By.xpath("//*[@id='app']//a[text()='我的博客']")).click();
waitTime(4);
String handle = getLastHandle(driver);
driver.switchTo().window(handle);
//循环主体,重复刷新页面访问博客
while(count<100){
driver.navigate().refresh();
waitTime(5);
driver.findElement(By.xpath("//*[@id='view-containe']//div/input[@placeholder='请输入关键词']")).clear();
driver.findElement(By.xpath("//*[@id='view-containe']//div/input[@placeholder='请输入关键词']")).sendKeys(blogName);
driver.findElement(By.xpath("//*[@id='view-containe']//button/span[text()='搜索']")).click();
waitTime(5);
String number = driver.findElement(By.xpath("//*[@id='view-containe']//div[@class='item-info-left']/span[3]")).getText();
System.out.println("当前访问量" + "("+count+"):" + number);
driver.findElement(By.xpath("//*[@id='view-containe']//a/span[text()='查看']")).click();
waitTime(19);
String handle1 = getLastHandle(driver);
driver.switchTo().window(handle1);
driver.close();//关闭窗口
String handle2 = getLastHandle(driver);
driver.switchTo().window(handle2);
waitTime(1);
// driver.navigate().back(); //返回第一次访问的网页,即后退功能
count++;
}
}
//正常关闭浏览器的方法
public static void close() {
// TODO Auto-generated method stub
if(driver != null){
driver.quit();
System.out.println("driver关闭");
} else {
System.out.println("未找到driver");
}
try {
taskkill();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("taskkill:::::::" + e);
e.printStackTrace();
}
}
//设置等待时间
public static void waitTime (long timeout){
try {
Thread.sleep(timeout*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//使用cmd命令清理环境,关闭浏览器和驱动
public static void taskkill() throws InterruptedException{
Runtime run =Runtime.getRuntime();
Process p;
try {
System.out.println("taskkill /f /im chromedriver.exe");
p = run.exec("taskkill /f /im chromedriver.exe");
p = run.exec("taskkill /f /im chrome.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取当前打开窗口的最后一个句柄
public static String getLastHandle(WebDriver driver) {
//获取当前打开窗口的所有句柄
Set<String> Allhandles = driver.getWindowHandles();
ArrayList<String> lst = new ArrayList<String>(Allhandles);
return lst.get(lst.size()-1);
}
}
2、不带账号刷访问量代码
和上边代码差不多,所以注释不多
如要测试代码,需要适配谷歌浏览器安装路径、driver路径和博客名称
代码说明:搜索到的博客要在第一个,当然也可以修改代码,不在第一个也行
package com.csdn.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class NoUser {
public static WebDriver driver = null;
public static int count = 1;
public static int countbig = 1;
public static void main(String[] args) {
// TODO Auto-generated method stub
while (true){
System.out.println("开始web自动化第" + countbig + "次循环!");
count=1;
driver=null;
countbig++;
waitTime(1);
try {
test();
close();
} catch (Exception e) {
System.out.println("捕获到异常:::" + e);
waitTime(3);
close();
}
}
}
public static void test(){
//启动浏览器的参数
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--allow-silent-push");
options.addArguments("--incognito");
options.addArguments("--v8-cache-options");
driver = new ChromeDriver(options);
System.out.println("开始test");
String dirverpath = System.getProperty("user.dir");
System.out.println("当前项目" + dirverpath);
System.setProperty(dirverpath + "\\src\\Drivers\\chromedriver.exe", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
waitTime(3);
String blogName = "博客名称";
driver.get("https://www.csdn.net/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
waitTime(1);
while(count<60){
driver.navigate().refresh();
waitTime(1);
// 第一次和后边用到的页面不一样,所以作区分
if (count == 1){
driver.findElement(By.xpath("//input[@id='toolbar-search-input']")).clear();
driver.findElement(By.xpath("//input[@id='toolbar-search-input']")).sendKeys(blogName);
driver.findElement(By.xpath("//input[@id='toolbar-search-input']")).sendKeys((Keys.ENTER));
waitTime(2);
String handle = getLastHandle(driver);
driver.switchTo().window(handle);
} else {
driver.findElement(By.xpath("//input[@id='toolbar-so-search-input']")).clear();
driver.findElement(By.xpath("//input[@id='toolbar-so-search-input']")).sendKeys(blogName);
driver.findElement(By.xpath("//input[@id='toolbar-so-search-input']")).sendKeys((Keys.ENTER));
}
// String url = driver.findElement(By.xpath("/html/body/div[4]/div[3]/div[2]/div[3]/div/div/dl[1]/a")).getAttribute("href");
//
// WebElement element = driver.findElement(By.tagName("body"));
// element.sendKeys(Keys.CONTROL + "t");
// driver.get(url);
waitTime(4);
// 页面可能会弹出让用户登录的弹窗,所以刷新页面
try{
driver.findElement(By.xpath("//div[@class='htmledit_views']/dl[1]/a")).click();
}catch(Exception e){
System.out.println("查找页面博客项目失败,刷新页面!");
driver.navigate().refresh();
waitTime(2);
driver.findElement(By.xpath("//div[@class='htmledit_views']/dl[1]/a")).click();
}
waitTime(5);
String handle1 = getLastHandle(driver);
driver.switchTo().window(handle1);
String number = null;
// 页面可能会弹出让用户登录的弹窗,所以刷新页面
try{
number = driver.findElement(By.xpath("//*[@id='mainBox']/main//span[@class='read-count']")).getText();
}catch(Exception e){
System.out.println("获取页面访问量失败,刷新页面!");
driver.navigate().refresh();
waitTime(2);
number = driver.findElement(By.xpath("//*[@id='mainBox']/main//span[@class='read-count']")).getText();
}
System.out.println("当前访问量" + "("+count+"):" + number);
waitTime(18);
String handle2 = getLastHandle(driver);
driver.switchTo().window(handle2);
driver.close();//关闭窗口
waitTime(1);
String handle3 = getLastHandle(driver);
driver.switchTo().window(handle3);
waitTime(1);
// driver.navigate().back(); //返回第一次访问的网页,即后退功能
count++;
}
}
public static void waitTime (long timeout){
try {
Thread.sleep(timeout*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close() {
// TODO Auto-generated method stub
if(driver != null){
driver.quit();
System.out.println("driver关闭");
} else {
System.out.println("未找到driver");
}
try {
taskkill();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("taskkill:::::::" + e);
e.printStackTrace();
}
}
public static void taskkill() throws InterruptedException{
Runtime run =Runtime.getRuntime();
Process p;
try {
System.out.println("taskkill /f /im chromedriver.exe");
p = run.exec("taskkill /f /im chromedriver.exe");
p = run.exec("taskkill /f /im chrome.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getLastHandle(WebDriver driver) {
//获取当前打开窗口的所有句柄
Set<String> Allhandles = driver.getWindowHandles();
ArrayList<String> lst = new ArrayList<String>(Allhandles);
return lst.get(lst.size()-1);
}
}
3、运行效果图
4、遇到的问题及解决办法
- 1、大循环第二次时,报错 Timed out waiting for driver server to start
因为一直跑代码不关闭浏览器和驱动,担心driver和chrome占用CPU和内存越来越多导致电脑卡,所以想跑一段时间清理一次资源,重新拉起driver和chrome跑,结果测试代码的时候遇到这个问题,搜了很多文章都没解决,都是建议chrome浏览器的版本和driver是不是对应,但是第一次运行时OK的,所以排除这个问题,最后抱着试试的心态,手动设置了driver的地址,结果代码OK了,到现在也不知道是什么原因,希望有大佬可以指导指导。
- 2、设置页面等待时间失效
也是搜了很多文章,什么隐式等待显示等待大概试了下(没仔细研究),好像都有问题,最后采用线程等待Thread.sleep(timeout)解决。可能不是selenium提供的等待方法不行,是我没弄懂。
- 3、遗留问题:CSDN博客每60s才会增加一次访问量,所以导致代码真实效率其实很低
CSDN的60s机制问题,暂不清楚这个机制的根据是什么进行限制(同一IP、同一账号还是同一博客),至今没有解决,望各位大佬交流解决办法。
欢迎各位留言交流
上一篇: Linux之进程管理【重要】