package com.zzsn.paser;


import com.zzsn.utility.index.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;


public class SeleniumTime {

	public ChromeOptions chromeOptions =new ChromeOptions() ;
	public  ChromeDriver driver;
	
	
	public SeleniumTime(){
//		 System.setProperty("webdriver.chrome.driver", "E:\\cmd\\chromedriver.exe");
//		 System.setProperty("webdriver.chrome.driver", "D:\\cmdvip\\chromedriver.exe");
//		 System.setProperty("webdriver.chrome.driver", "E:\\chrome\\chromedriver.exe");
		 System.setProperty("webdriver.chrome.driver", Constants.CHROMEDRIVE);
		 chromeOptions.addArguments("blink-settings=imagesEnabled=false");
//		 chromeOptions.addArguments("--headless");
		 driver = new ChromeDriver(chromeOptions);
	}
	/**
	 * 根据网址获取网页html信息
	 * @param url
	 * @return
	 */
	public  String getScopehtml(String url){
//=====================================================================================================		
//		ChromeOptions chromeOptions =new ChromeOptions();
////		System.setProperty("webdriver.chrome.driver", Constants.CHROMEDRIVE);
//		System.setProperty("webdriver.chrome.driver", "D:\\project\\cmd\\chromedriver.exe");
//		//System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"); 
//		//chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
//		//C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe
//		//C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
//		//chromeOptions.addArguments("--headless");
//		ChromeDriver driver = new ChromeDriver(chromeOptions);
//=====================================================================================================		
		try{
			driver.get(url);
	        WebElement webElement = driver.findElement(By.xpath("/html"));
	        try{
	        	Thread.sleep(3000l);
	        	String html = webElement.getAttribute("outerHTML");
	        	 Thread.sleep(5000l);
		        driver.quit();
		       // System.out.println(html);
		        if(url.contains("http://www.flw.ph")){
		        	String a = "<div class=\"attach_nopermission attach_tips\">";
		        	String b = "<span class=\"atips_close\" onclick=\"this.parentNode.style.display='none'\">x</span>";
		        	if(html.contains(a)&&html.contains(b)){
		        		String[] split = html.split(a);
		        		String sa = split[0];
		        		String[] split2 = split[1].split(b);
		        		String sb = split2[1];
		        		String substring = sb.substring(7);
		        		String sab = sa + substring ;
		        		return sab;
		        	}
		        }
		        return html;
	        }catch(Exception e){
	        	System.out.println("动态爬取方式一出现+"+"org.openqa.selenium.StaleElementReferenceException异常"
	        +"可能原因为过快的执行没有找到指定的页面元素");
	        	System.out.println("=============执行方法二==============");
	        	Thread.sleep(3000l);
		        String html = driver.getPageSource();
		        Thread.sleep(5000l);
		        	driver.quit();
		        	 if(url.contains("http://www.flw.ph")){
				        	String a = "<div class=\"attach_nopermission attach_tips\">";
				        	String b = "<span class=\"atips_close\" onclick=\"this.parentNode.style.display='none'\">x</span>";
				        	if(html.contains(a)&&html.contains(b)){
				        		String[] split = html.split(a);
				        		String sa = split[0];
				        		String[] split2 = split[1].split(b);
				        		String sb = split2[1];
				        		String substring = sb.substring(7);
				        		String sab = sa + substring ;
				        		return sab;
				        	}
				        }
		        	return html;
	        }
//	        Thread.sleep(3000l);
//	        String source = driver.getPageSource();
//	        //if(source.length()!=0){
//	        	driver.quit();
//	        	return source;
	        //}
//	        String html = webElement.getAttribute("outerHTML");
//	        //System.out.println(html);
//	        driver.quit();
//	        return html;
//==========================================================================
//			driver.get(url);
//	        // 休眠1s,为了让js执行完
//	        Thread.sleep(1000l);
//	        // 网页源码
//	        String source = driver.getPageSource();
//	        System.out.println("进入SeleniumTime中的getScopehtml方法获取相应的html");
//	        driver.quit();
//	        return source;
		}catch(Exception e){
			 try {
				Thread.sleep(5000l);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			driver.quit();
			e.printStackTrace();
		}
		 try {
			Thread.sleep(5000l);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.quit();
		return null;
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
		
		//去除html中的相关标签
		/**
		 * 网上大多是说明直接使用正则表达式不能很好的适用于html
		 * 经过尝试我无法删除先关div中内容，只能自己通过字符串切割的形式获取
		 */
		
		SeleniumTime s = new SeleniumTime();
		String scopehtml = s.getScopehtml("http://www.flw.ph/thread-869016-1-1.html");
		
		String a = "<div class=\"attach_nopermission attach_tips\">";
		String b = "<span class=\"atips_close\" onclick=\"this.parentNode.style.display='none'\">x</span>";
		System.out.println("开始");
		if(scopehtml.contains(a)){
			System.out.println("包含a");
		}
		if(scopehtml.contains(a)){
			System.out.println("包含b");
		}
		System.out.println("结束");
		
		String[] split = scopehtml.split(a);
		String sa = split[0];
		System.out.println("首次截取的长度"+split.length);
		String[] split2 = split[1].split(b);
		String sb = split2[1];
		String substring = sb.substring(7);
		System.out.println("再次截取的长度"+split2.length);
		String sab = sa + substring ;
		
//		//解决方式    正则匹配删除标签
//		//   *.div[class="t_fsz"]
//		String regex = "<div class=\"attach_nopermission attach_tips\">(.*?)</div>";
//		//String regex = "<div.*?>(.*?)</div>";
//		//String regex = "*.div[class="+"attach_nopermission attach_tips"+"]";
//		
////		 boolean isMatch = regex.matches(scopehtml);
////	      System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
////		
//	   // 创建 Pattern 对象
//	      Pattern r = Pattern.compile(regex);
//	 
//	      // 现在创建 matcher 对象
//	      Matcher m = r.matcher(scopehtml);
//	      if (m.find( )) {
//	          System.out.println("Found value: " + m.group(0) );
//	          System.out.println("Found value: " + m.group(1) );
//	          System.out.println("Found value: " + m.group(2) );
//	          System.out.println("Found value: " + m.group(3) ); 
//	       } else {
//	          System.out.println("NO MATCH");
//	       }
//		
//		
		 File file = new File("D:/123.txt");
         try {
			PrintStream ps = new PrintStream(new FileOutputStream(file));
			ps.println(sab);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
