phantomjs selenium实现刷搜索引擎,刷百度来路,刷点击,真实有效果
来源:互联网 发布:淘宝注册资金 编辑:程序博客网 时间:2024/05/19 22:44
时间仓促,先把之前的测试代码贴上
/* * 方案一 模拟搜索百度(打开百度,搜索,下三页,重新搜索,点击前三条任意一条 */public static void run_one(String search_one, String search_two, int mysqlid){System.out.println(search_two);Map map = PhoneAgen.get_userAgent();//Map map = get_userAgent();String userAgent = (String) map.get("userAgent");DesiredCapabilities capabilities = null;capabilities = DesiredCapabilities.android();int phone_r_num = (int)map.get("phone_r_num");if(phone_r_num<=1){capabilities.setBrowserName("iPhone");capabilities.setPlatform(Platform.MAC);}else{capabilities.setBrowserName("android");capabilities.setPlatform(Platform.ANDROID);}//System.out.println(capabilities.getBrowserName());//System.out.println(capabilities.getVersion());//System.out.println(capabilities.getPlatform());capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);PhantomJSDriver dr = new PhantomJSDriver(capabilities);dr.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);try {Dimension POPULAR_DISPLAY_SIZE = new Dimension( (int)map.get("p_w"), (int) map.get("p_h"));dr.manage().window().setSize(POPULAR_DISPLAY_SIZE);System.out.println("打开百度");dr.get("http://m.baidu.com/");//((JavascriptExecutor) dr).executeScript("window.scrollBy(0, 700)"); //System.out.println(dr.getPageSource());//Thread.sleep(3000);//snapshot((TakesScreenshot)dr, "1.png");WebElement searchBox = dr.findElement(By.id("index-kw")); searchBox.sendKeys(search_one); System.out.println("在搜索框中输入文字"); Thread.sleep(1000); WebElement searchButton = dr.findElement(By.className("se-bn")); //WebElement searchButton = dr.findElement(By.id("index-bn")); searchButton.submit();; System.out.println("搜索点击等待"); Thread.sleep(4000); //snapshot((TakesScreenshot)dr, "2.png");System.out.println("当前地址(第一页):"+dr.getCurrentUrl());//获取高度,用来模拟鼠标滚动String page_height = ((JavascriptExecutor) dr).executeScript("return document.body.scrollHeight").toString(); int page_height_f = Integer.parseInt(page_height)-650;int mouse_scroll_num = mt_rand(4,5);for(int i=1;i<=mouse_scroll_num;i++){int page_height_cur = (int) (page_height_f/mouse_scroll_num);int page_height_cur_n = mt_rand(0,50);if(mt_rand(1,2)==1){page_height_cur -= page_height_cur_n;}else{page_height_cur += page_height_cur_n;}((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+page_height_cur+")");System.out.println("进入鼠标滚动(第一页)"+i+"--"+page_height_cur);Thread.sleep(mt_rand(1,3)*1000);}System.out.println("准备点击下一页");WebElement searchPage_one_next = dr.findElement(By.className("new-nextpage-only")); //点击下一页 searchPage_one_next.click();//--------------------------------------------------------------Thread.sleep(3500);System.out.println("点击下一页后等待");//snapshot((TakesScreenshot)dr, "3.png");System.out.println("当前地址(第二页):"+dr.getCurrentUrl());String page_height2 = ((JavascriptExecutor) dr).executeScript("return document.body.scrollHeight").toString(); int page_height_f2 = Integer.parseInt(page_height2)-650;int mouse_scroll_num2 = mt_rand(4,5);for(int i=1;i<=mouse_scroll_num2;i++) {int page_height_cur2 = (int) (page_height_f2/mouse_scroll_num2);int page_height_cur_n2 = mt_rand(0,50);if(mt_rand(1,2)==1){page_height_cur2 -= page_height_cur_n2;}else{page_height_cur2 += page_height_cur_n2;}((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+page_height_cur2+")");System.out.println("进入鼠标滚动(第二页)"+i+"--"+page_height_cur2);Thread.sleep(mt_rand(1,3)*1000);}System.out.println("准备点击下一页");WebElement searchPage_two_next = dr.findElement(By.className("new-nextpage")); //点击下一页 searchPage_two_next.click();//--------------------------------------------------------------Thread.sleep(3500);System.out.println("点击下一页后等待");System.out.println("当前地址(第三页):"+dr.getCurrentUrl());//snapshot((TakesScreenshot)dr, "4.png");Thread.sleep(2000);//重新搜索词WebElement searchPage_text = dr.findElement(By.id("kw"));searchPage_text.clear();searchPage_text.sendKeys(search_two);Thread.sleep(1000);WebElement searchPage_se_bn = dr.findElement(By.className("se-bn"));searchPage_se_bn.click();//--------------------------------------------------------------Thread.sleep(4000);System.out.println("重新搜索当前地址:"+dr.getCurrentUrl());//snapshot((TakesScreenshot)dr, "5.png");//成功增加记录String sql1="Update search Set num=num+1 Where id="+mysqlid;Dbhelper.executeNonQuery(sql1); WebElement search_r1 = null;//随机点击前三个链接中的任意一个try{search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div[1]/div/div/div/a[1]"));int last_height = mt_rand(300,500);((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+last_height+")");System.out.println("找到广告,滚动"+last_height);}catch(Exception e){int click_num = mt_rand(2,3); search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div["+click_num+"]/div[1]/a"));System.out.println("没有广告,"+click_num);}search_r1.click();Thread.sleep(4000);System.out.println("打开搜索地址:"+dr.getCurrentUrl());//snapshot((TakesScreenshot)dr, "6.png");dr.quit();} catch (Exception e) {dr.quit();e.printStackTrace();}finally {dr.quit();}}
/* * 方案三 搜索一次 过10-30随机秒 再次搜索 */public static void run_three(String search_one, String search_two, int mysqlid){System.out.println(search_two);Map map = PhoneAgen.get_userAgent();String userAgent = (String) map.get("userAgent");DesiredCapabilities capabilities = null;capabilities = DesiredCapabilities.android();int phone_r_num = (int)map.get("phone_r_num");if(phone_r_num<=1){capabilities.setBrowserName("iPhone");capabilities.setPlatform(Platform.MAC);}else{capabilities.setBrowserName("android");capabilities.setPlatform(Platform.ANDROID);}//System.out.println(capabilities.getBrowserName());//System.out.println(capabilities.getVersion());//System.out.println(capabilities.getPlatform());capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);PhantomJSDriver dr = new PhantomJSDriver(capabilities);dr.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);try {Dimension POPULAR_DISPLAY_SIZE = new Dimension( (int)map.get("p_w"), (int) map.get("p_h"));dr.manage().window().setSize(POPULAR_DISPLAY_SIZE);System.out.println("打开百度");dr.get("http://m.baidu.com/");//((JavascriptExecutor) dr).executeScript("window.scrollBy(0, 700)"); //System.out.println(dr.getPageSource());//Thread.sleep(3000);//snapshot((TakesScreenshot)dr, "1.png");WebElement searchBox = dr.findElement(By.id("index-kw")); searchBox.sendKeys(search_one); System.out.println("在搜索框中输入文字"); Thread.sleep(1000); //By.xpath(xpathExpression) //*[@id="index-bn"] WebElement searchButton = dr.findElement(By.className("se-bn")); searchButton.submit();; System.out.println("搜索点击等待"); Thread.sleep(4000); //snapshot((TakesScreenshot)dr, "2.png");System.out.println("当前地址(第一页):"+dr.getCurrentUrl());//获取高度,用来模拟鼠标滚动String page_height = ((JavascriptExecutor) dr).executeScript("return document.body.scrollHeight").toString(); int page_height_f = Integer.parseInt(page_height)-650;int mouse_scroll_num = mt_rand(4,5);for(int i=1;i<=mouse_scroll_num;i++){int page_height_cur = (int) (page_height_f/mouse_scroll_num);int page_height_cur_n = mt_rand(0,50);if(mt_rand(1,2)==1){page_height_cur -= page_height_cur_n;}else{page_height_cur += page_height_cur_n;}((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+page_height_cur+")");System.out.println("进入鼠标滚动(第一页)向下"+i+"--"+page_height_cur);Thread.sleep(mt_rand(1,3)*1000);}int mouse_scroll_num2 = mt_rand(4,5);for(int i=1;i<=mouse_scroll_num2;i++){int page_height_cur = (int) (page_height_f/mouse_scroll_num);int page_height_cur_n = mt_rand(0,50);if(mt_rand(1,2)==1){page_height_cur -= page_height_cur_n;}else{page_height_cur += page_height_cur_n;}((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+(-page_height_cur)+")");System.out.println("进入鼠标滚动(第一页)向上"+i+"--"+page_height_cur);Thread.sleep(mt_rand(1,3)*1000);}//重新搜索词WebElement searchPage_text = dr.findElement(By.id("kw"));searchPage_text.clear();searchPage_text.sendKeys(search_two);Thread.sleep(1000);WebElement searchPage_se_bn = dr.findElement(By.className("se-bn"));searchPage_se_bn.click();//--------------------------------------------------------------Thread.sleep(4000);System.out.println("重新搜索当前地址:"+dr.getCurrentUrl());//snapshot((TakesScreenshot)dr, "5.png");//成功增加记录String sql1="Update search Set num=num+1 Where id="+mysqlid;Dbhelper.executeNonQuery(sql1); //随机点击前三个链接中的任意一个WebElement search_r1 = null;//随机点击前三个链接中的任意一个try{search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div[1]/div/div/div/a[1]"));int last_height = mt_rand(300,500);((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+last_height+")");System.out.println("找到广告,滚动"+last_height);}catch(Exception e){int click_num = mt_rand(2,3); search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div["+click_num+"]/div[1]/a"));System.out.println("没有广告,"+click_num);}search_r1.click();Thread.sleep(4000);System.out.println("打开搜索地址:"+dr.getCurrentUrl());//snapshot((TakesScreenshot)dr, "6.png");dr.quit();} catch (Exception e) {dr.quit();e.printStackTrace();}finally {dr.quit();}}
阅读全文
1 0
- phantomjs selenium实现刷搜索引擎,刷百度来路,刷点击,真实有效果
- selenium phantomjs
- selenium+phantomjs
- Selenium+phantomJS
- selenium+ phantomjs实现动态网页爬取
- selenium phantomjs 实现新浪微博爬虫
- python+selenium+phantomjs实现爬虫功能
- selenium模拟刷百度流量源码
- selenium 和 PhantomJS,BeautifulSoup 百度知道爬虫实战
- 利用javascript判断搜索引擎来路
- 基于Python,Selenium和PhantomJS实现动态页面爬取
- python基于selenium+PhantomJS实现搜索交互案例
- Selenium + PhantomJS + python 简单实现爬虫的功能
- 基于selenium-java封装chrome、firefox、phantomjs实现爬虫
- python基于selenium+PhantomJS实现用提交表单,并点击按钮功能,获取返回的页面源码和ur等信息l。
- selenium简单模拟百度搜索点击器
- android ListView实现颜色交替并且有点击效果
- iOS UICollectionViewCell上有Button点击实现收藏效果
- idea2017的获取注册码的方法
- Leetcode 200 Number of Islands
- 1.4 自定义框架
- java进阶(三) 用单例模式加载配置文件
- 爬虫总结 && 部分正则匹配
- phantomjs selenium实现刷搜索引擎,刷百度来路,刷点击,真实有效果
- Java集合HashSet的hashcode方法引起的内存泄漏问题
- windows下python安装numpy模块
- package.json的 ^~
- OD学习笔记
- 卷积算子计算方法(卷积运算)
- JavaWeb 学习笔记(四) ServletContext
- sqlserver 根据表名查询 该表的主键是那些表的外键
- backup命令