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