擺脫了“心急吃不了熱豆腐”的無頭蒼蠅狀態之後,在正式開始認真研究爬蟲之前,爲了不至於沒東西交差,先用selenium模仿着寫了一個“類爬蟲”。
爲什麼選擇selenium:selenium是一款自動化的測試工具,可以模擬用戶對瀏覽器的操作。這裏想強調的是完全的模擬,你甚至可以把他想象爲按鍵精靈。。。。。。。(勿噴,我是菜鳥),當然它還有很多很強大的功能,我暫時沒有接觸到。但是對於要寫爬蟲的我來說,最大的優勢就是其繞過了“前端渲染”這種頁面(即用ajax異步請求數據),因爲這種頁面用普通的爬蟲是沒辦法扒到數據的,必須要用http請求來模擬ajax請求balabalabala...(正在學習模擬請求),或者是爬蟲結合selenium這種自動化的測試工具一起上。
本次測試的網站爲 -》www.pinterest.com 一個圖片網站,應該爲無限的瀑布流網站.....故肯定是採用的ajax請求。
另其登錄頁面 和 瀏覽界面的網址是不變的(即是在www.pinterest.com這個網站登錄,同樣也是在www.pinterest.com進行瀏覽。),所以需要獲取cookie來進行操作。
先貼代碼:
public class Test {
public static void main(String[] args) throws InterruptedException {
/*
* 不同瀏覽器的設置不同,推薦使用火狐瀏覽器。不推薦使用IE瀏覽器。
* 此處我用的火狐版本爲40.4,selenium版本爲2.53.1
*/
System.setProperty("webdriver.firefox.bin", "D:/火狐瀏覽器/firefox.exe");
System.out.println("start");
WebDriver driver = new FirefoxDriver();
/*
* 由於要設置cookie,所以在設置之前必須就建立連接。
* 否則會報錯。Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
* 故第一句get是不能少的。
*/
driver.get("https://www.pinterest.com/");
for(Cookie cookie:Test.click("你的賬號", "你的密碼")){
driver.manage().addCookie(cookie);
}
driver.manage().window().maximize();
driver.get("https://www.pinterest.com/");
/*
* 此處我使用set來去重,當然不是什麼好辦法。只是這個版本只是最初版
* 之後會再優化
*/
Set<String> result = new HashSet<String>();
int i = 0;
int num = 4600;
while(true){
System.out.println(i++);
Thread.sleep(1000*20); //拖動滾動條之後,休眠,便於網站加載數據
List<WebElement> list = driver.findElements(By.cssSelector("img.pinImg.fullBleed.loaded.fade"));
for(WebElement webElement:list){
String url = webElement.getAttribute("src");
System.out.println("url:"+url);
result.add(url);
}
System.out.println("result.size = "+result.size());
if(list.size() == 0){
break;
}
int roll = num*i;
System.out.println("roll:"+roll);
/*
* 這個方法模擬向下拖動 垂直滾動條
*/
String setscroll = "document.documentElement.scrollTop=" + "" + roll;
JavascriptExecutor jse=(JavascriptExecutor) driver;
jse.executeScript(setscroll);
}
driver.close();
System.out.println("end");
}
/**
* 獲取cookie
* @param username
* @param password
* @return
* @throws InterruptedException
*/
public static Set<Cookie> click(String username,String password) throws InterruptedException{
System.setProperty("webdriver.firefox.bin", "D:/火狐瀏覽器/firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.pinterest.com/");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Thread.sleep(1000*8);
driver.findElement(By.xpath(".//*[@id='userEmail']")).clear();
driver.findElement(By.xpath(".//*[@id='userEmail']")).sendKeys(username);
driver.findElement(By.xpath(".//*[@id='userPassword']")).clear();
driver.findElement(By.xpath(".//*[@id='userPassword']")).sendKeys(password);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/div/div/div[2]/div[3]/div[1]/div[3]/div/div[1]/div/div[2]/form/div/ul/div[1]/div[2]/li[2]/div/button")).click();
Thread.sleep(1000*10);
Set<Cookie> cookies = driver.manage().getCookies();
System.out.println("Cookie.size = " + cookies.size());
driver.close();
return cookies;
}
}
在代碼相應處填寫你的賬號和密碼。
首先調用一個方法,獲取cookie。
然後再利用cookie進行登錄
在利用cookie的時候要注意這個錯誤,解決方法已經寫在代碼裏了。
Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
當然你直接在登錄好的頁面進行操作也可以。
之後就是你該幹嘛幹嘛。
缺點是效率很低下。
繼續研究爬蟲,隨時分享進度~
歡迎各位指正交流。