如何从其他网站获取数据?

时间:2014-04-14 07:10:19

标签: java selenium selenium-webdriver

我正在尝试从网站上获取数据而我能够做到这一点,但我的问题是 当我从网站上获取不同商品的数据时,我必须每次都在我的代码中更改,我想立即获取以下商品的数据,我该如何实现我的输出

商品

'Rice'
'Jwar'
'Corn'
'Matar'
'Chana'

这是我的代码

import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.util.StringTokenizer;
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.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;


public class Getdata1 {

 public static void main(String args[]) throws {


 int j=0;


      WebDriver driver = new HtmlUnitDriver(BrowserVersion.getDefault());
        String sDate = "27/03/2014";
        String commodity="Jo";

        String url="http://www.upmandiparishad.in/commodityWiseAll.aspx";
        driver.get(url);
        Thread.sleep(5000);

        new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText(commodity);
         driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);

        Thread.sleep(3000);
        driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
        Thread.sleep(5000);


        WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
       // WebElement find=driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"));
        String htmlTableText = findElement.getText();

    htmlTableText=htmlTableText.replace("S.No.DistrictMarketPrice","");
   htmlTableText= htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "\n$1");
  htmlTableText = htmlTableText.replaceAll("(?=(.*?[ ]){4,}).*?[\n\r]", "");
   System.out.println(htmlTableText);


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

    }
}

1 个答案:

答案 0 :(得分:0)

您可能需要将商品发送为command line argumentinput from user