【问题标题】:scrape data from website?从网站上抓取数据?
【发布时间】:2014-04-02 07:04:22
【问题描述】:

嗨,我正在尝试抓取网站的数据,我为此编写了代码,我想在按钮单击事件上抓取数据,但是当我运行我的程序时它会抛出异常

例外是:java.lang.NoClassDefFoundError: com/google/common/base/Function

如何删除此异常并运行我的程序

这是我尝试过的代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class GetData {

    public static void main(String args[]) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.upmandiparishad.in/commodityWiseAll.aspx");
        Thread.sleep(5000);
        // select barge
        new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
        // click button
        Thread.sleep(3000);
        driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
        Thread.sleep(5000);

        //get only table tex
        WebElement findElement = driver.findElement(By.className("grid-view"));
        String htmlTableText = findElement.getText();
        // do whatever you want now, This is raw table values.
        System.out.println(htmlTableText);

        driver.close();
        driver.quit();

    }
}

提前致谢

【问题讨论】:

    标签: java web-scraping jsoup


    【解决方案1】:

    希望对你有帮助..

        //select barge
        new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
    
        String sDate = "12/04/2014"; //What date you want
        driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
    
        driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
        Thread.sleep(3000);
    
        WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
        String htmlTableText = findElement.getText();
        // do whatever you want now, This is raw table values.
        System.out.println(htmlTableText);
    

    【讨论】:

    • 我怎样才能得到生成的网页?
    • 对不起,我忘记了 ; String sDate = "12/04/2014"; 有了这些修复它正在工作..我试过了..
    • 使用String.split(" ")。按 分割; String arrRowData[] = htmlTableText.split("\n"); for(int i = 0; i < arrRowData.length(); i++){ String arrCellData[] = arrRowData[i].split(" "); for(int j = 0; j < arrCellData.length(); j++) { sout(arrCellData[j]);}}
    • 亲爱的你能告诉我不打开浏览器如何获得相同的数据
    • 试试 HtmlUnitDriver
    猜你喜欢
    • 2013-03-14
    • 2013-05-21
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多