我正在開發一個項目MAVEN使用Java。 我必須得到一個URL,向下滾動,並獲得給定網頁中其他項目的所有鏈接。直到現在,我使用Selenium動態獲取頁面,並向下滾動頁面,並獲取鏈接。但這需要太多時間。請幫我優化一下。如何在java中使用硒優化向下滾動代碼
例如: - ,我在頁面上工作,其鏈接是here。
我的問題: - 使用硒
- 滾動網頁很慢。我怎樣才能優化這個? (推薦任何其他方法
做同樣或者幫我優化這一塊)提前
感謝。尋找你的善良迴應。
代碼,以動態獲取和滾動頁面: -
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import com.google.common.collect.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
/**
*
* @author jhamb
*/
public class Scroll_down {
private static FirefoxProfile createFirefoxProfile() {
File profileDir = new File("/tmp/firefox-profile-dir");
if (profileDir.exists()) {
return new FirefoxProfile(profileDir);
}
FirefoxProfile firefoxProfile = new FirefoxProfile();
File dir = firefoxProfile.layoutOnDisk();
try {
profileDir.mkdirs();
FileUtils.copyDirectory(dir, profileDir);
} catch (IOException e) {
e.printStackTrace();
}
return firefoxProfile;
}
public static void main(String[] args) throws InterruptedException{
String url1 = "http://www.jabong.com/men/shoes/men-sports-shoes/?source=home-leftnav";
System.out.println("Fetching %s..." + url1);
WebDriver driver = new FirefoxDriver(createFirefoxProfile());
driver.get(url1);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
for (int second = 0;; second++) {
if (second >= 60) {
break;
}
jse.executeScript("window.scrollBy(0,200)", "");
Thread.sleep(1000);
}
String hml = driver.getPageSource();
driver.close();
Document document = Jsoup.parse(hml);
Elements links = document.select("div");
for (Element link : links) {
System.out.println(link.attr("data-url"));
}
}
}
@請幫助我。向下滾動過程需要大量時間。 – devsda 2013-04-11 08:42:37
滾動有什麼好處?它有什麼必要? – 2013-04-11 08:54:15
@EliranMalka我正在致力於onlne購物網站的數據,如亞馬遜,Flipkart等。而且,當我們向下滾動頁面時,頁面顯示更多產品,所以如果我給出任何URL,那麼它會滾動到結束,然後獲取所有的鏈接,它存在於它。我編寫了這整個功能,但這是非常緩慢的。告訴我如何在我的代碼中優化這個scrollong部分。請。 – devsda 2013-04-11 09:01:20