`
Mr_Tank_
  • 浏览: 21245 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

使用Selenium渲染页面并提取数据

阅读更多

最近使用java爬虫收集数据,遇到js动态数据的时候使用如jsoup是获取不到数据的,所以要先进行页面的渲染。

下面是以京东商城数据为例,使用Selenium需要先下好相应的驱动,我使用的是Chrome;还需要加入common-exec包

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


/**
 * Created with IntelliJ IDEA.
 * User: Mr_Tank_
 * Date: 13-11-29
 * Time: 下午9:52
 * To change this template use File | Settings | File Templates.
 */
public class seleniumTest {


    public static void main(String args[]){
        System.getProperties().setProperty("webdriver.chrome.driver", "E:\\driver\\chromedriver.exe");
        WebDriver webDriver =new ChromeDriver();
        webDriver.get("http://list.jd.com/9987-653-655-0-0-0-0-0-0-0-1-1-1-1-1-72-4137-33.html");

        WebElement webElement = webDriver.findElement(By.xpath("//div[@id='plist']"));

        System.out.println(webElement.getAttribute("outerHTML"));

        WebElement li=webElement.findElement(By.xpath("//li[@index='1']"));

        String name=li.findElement(By.xpath("//li[@index='1']//div[@class='p-name']/a")).getText();
        System.out.println("商品名:"+name);

        String price=li.findElement(By.xpath("//li[@index='1']//div[@class='p-price']/strong")).getText();
        System.out.println("价格:"+price);

        String eva=li.findElement(By.xpath("//li[@index='1']//span[@class='evaluate']/a[@target='_blank']")).getText();
        System.out.println("评价:"+eva);

        webDriver.close();
    }
}

结果:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics