网页数据抓取

1.常规解析html

使用Jsoup包,配合开发者工具(F12)定位需要的数据路径。

 数据抓取往往流程类似,可以提出相似流程,优化结构。

2. selenium   实例参考https://my.oschina.net/dyhunter/blog/94090

geckodriver安装:参考http://blog.csdn.net/jinhe123/article/details/69946234,到官网上下载与系统相应的最新版本geckodriver:https://github.com/mozilla/geckodriver/releases

a. 安装firefox。

b. F12打开开发者工具,定位到页面元素,在查看器内右键->复制->xPath路径,也可以通过By.***查找。

public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "C:\setup\geckodriver.exe");
        // 创建一个 FireFox 的浏览器实例
        WebDriver driver = new FirefoxDriver();

        // 让浏览器访问
        driver.get("http://www.shfe.com.cn/bourseService/businessdata/summaryinquiry/index.html?paramid=currinstrumentprop");

        // 获取 网页的 title
        System.out.println("1 Page title is: " + driver.getTitle());

        // 点击修改日期
        // 2013年
        WebElement ymd = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[1]/div[2]/div[2]/div/div/div/div/select[1]/option[10]"));
        ymd.click();
        // 10月
        ymd = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[1]/div[2]/div[2]/div/div/div/div/select[2]/option[10]"));
        ymd.click();
        // 11日
        ymd = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[1]/div[2]/div[2]/div/div/table/tbody/tr[3]/td[3]/a"));
        ymd.click();

        // 通过 xpath找到 input 的 DOM
        // 结算参数
        WebElement ele = driver.findElement(By.xpath("//*[@id="settlement"]"));
        ele.click();
        // //*[@id="addedtable"]
        // /html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[1]/tr
        // /html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[1]
        // /html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[3]/tr
        // /html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[26]
        // /html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[4]/tr/td[1]
        WebElement table = driver.findElement(By.xpath("//*[@id="addedtable"]"));
        int count = table.findElements(By.tagName("tbody")).size();
        for (int i = 2; i <= count; i++) {
            String path = "/html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[" + i + "]/tr";
            WebElement tr = driver.findElement(By.xpath(path));
            if (tr == null) break;
            int size = tr.findElements(By.tagName("td")).size();
            for (int j = 1; j <= size; j++) {
                WebElement td = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div[3]/table/tbody[" + i + "]/tr/td[" + j + "]"));
                System.out.print(td.getText() + "  ");
            }
            System.out.println();
        }
        List<WebElement> es = driver.findElements(By.className("title"));
        for (WebElement webElement : es) {
            System.out.println(webElement.getText());
        }

        // 显示搜索结果页面的 title
        System.out.println("2 Page title is: " + driver.getTitle());

        // 关闭浏览器
        driver.quit();
    }

使用selenium有时候打开网页慢的话,有时候会导致读取数据报错,可以尝试sleep等待一会,再读取数据。

原文地址:https://www.cnblogs.com/starRebel/p/7130918.html