Java tutorial
/* * Copyright (C) 2015 hu * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package cn.edu.hfut.dmic.webcollector.example; import cn.edu.hfut.dmic.webcollector.crawldb.DBManager; import cn.edu.hfut.dmic.webcollector.crawler.Crawler; import cn.edu.hfut.dmic.webcollector.fetcher.Executor; import cn.edu.hfut.dmic.webcollector.model.CrawlDatum; import cn.edu.hfut.dmic.webcollector.model.CrawlDatums; import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BerkeleyDBManager; import java.util.List; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; /** * WebCollector?javascript?? * * @author hu */ public class DemoSelenium { static { //?Selenium Logger logger = Logger.getLogger("com.gargoylesoftware.htmlunit"); logger.setLevel(Level.OFF); } public static void main(String[] args) throws Exception { Executor executor = new Executor() { @Override public void execute(CrawlDatum datum, CrawlDatums next) throws Exception { HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(true); driver.get(datum.getUrl()); List<WebElement> elementList = driver.findElementsByCssSelector("h3.vrTitle a"); for (WebElement element : elementList) { System.out.println("title:" + element.getText()); } } }; //DBDBManager DBManager manager = new BerkeleyDBManager("crawl"); //Crawler?DBManagerExecutor Crawler crawler = new Crawler(manager, executor); crawler.addSeed("https://www.sogou.com/web?query=%E6%B7%98%E5%AE%9D"); crawler.start(1); } }