2014-04-04 62 views
-2

嗨,我想从另一个网站,其中我能够做的,但问题是,我想提取我的数据,我所希望的格式,我不能够实现,所以我如何能实现我的目标中提取数据如何使用正则表达式从其他网站提取数据?

这里是我的代码,我做到了

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; 
import java.sql.*; 

public class Getdata2 { 

    Statement st=null; 
    Connection cn=null; 
    public static void main(String args[]) throws InterruptedException, ClassNotFoundException, SQLException { 

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

     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("Jo"); 
     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")); 
     String htmlTableText = findElement.getText(); 
     // do whatever you want now, This is raw table values. 
     htmlTableText=htmlTableText.replace("S.No.DistrictMarketPrice",""); 
     System.out.println(htmlTableText); 


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

    } 
} 

我想提取这样

1 Agra Achhnera NIL 
2 Agra Agra NIL 
3 Agra Fatehabad NIL 
4 Agra FatehpurSikri NIL 
5 Agra Jagner NIL 
6 Agra Jarar NIL 
7 Agra Khairagarh NIL 
8 Agra Shamshabad NIL 
9 Aligarh Atrauli NIL 
10 Aligarh Chharra NIL 
11 Aligarh Aligarh 1300.00 
12 Aligarh Khair 1300.00 
13 Allahabad Allahabad NIL 
14 Allahabad Jasra NIL 
15 Allahabad Leriyari NIL 
16 Allahabad Sirsa NIL 
17 AmbedkarNagar Akbarpur NIL 
18 Ambedkar Nagar TandaAkbarpur NIL 

我的数据我如何能实现我的期望输出

在此先感谢

+0

的可能重复【如何做网页使用htmlunitsriver刮?](http://stackoverflow.com/questions/22807527/how-to-do-web-scraping-using-htmlunitsriver) – Nadun

+1

多个账户怎么办你有?这是为什么? – Nadun

+0

我不知道为什么我的账户被封锁了7天,所以我不得不抱歉 – user3496498

回答

1

注意:您不需要正则表达式。 Selenium本身提供了很好的工具来从表格中提取数据。

让我们来分析一下。从该网站查看源代码......这是它安排的方式。

<table id="ctl00_ContentPlaceHolder1_GridView1"> 
    <tbody> 
     <tr> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
     </tr> 
     ... more <trs> 
</table> 
  • 首先,你得到 “表行”。
  • 这是通过使用findElementfindElements完成的。

(以下代码是一个例子,根据代码修改)通过每个List<WebElement>元素,您在上面得到的

List<WebElement> tableRows = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1")).findElements(By.xpath(".//tbody/tr")); 
  • 立即循环。

你做到这一点使用

for (WebElement tableRow : tableRows) { 
... 
} 
  • 接下来,每个表行有4项(即4表格单元格)。
  • 再次使用findElements如上所示。
  • 商店这在List<WebElement>(再次如上所示)

代码:

tableRow.findElements(By.xpath(".//td") 
  • 现在,通过每个<td> WebElement循环。
  • 通过在每个WebElement上调用.getText()方法来获取每个元素中的文本。
  • 根据您的需要设置文本输出的格式。
+0

亲爱的,如果能做到这一点,我们会很帮助完整我 – user3496498

+1

我相信我已经回复你的答案。在StackOverflow中,预计你也可以自己做一些工作。 – Vish

+0

我正在做同样的事情,但数据不提取 – user3496498