2015-06-24 46 views
2

我有this html頁面,並在頁面的內容看起來像下面如何提取HTML表格內容的DataTable

enter image description here

我想在頁面的內容讀取到數據表和顯示器它在

<a href='/exodus-5.1/bacon/exodus-5.1-20150612-NIGHTLY-bacon.zip'>exodus-5.1-20150612-NIGHTLY-bacon.zip</a> 

我需要得到該鏈接的名稱,以及該URI太網格

例如

名稱: - exodus-5.1-20150612-NIGHTLY-bacon.zip
URI: - /exodus-5.1/bacon/exodus-5.1-20150612-NIGHTLY-bacon.zip

以下是我已經結束了

Dim request As HttpWebRequest = HttpWebRequest.Create(url) 
request.Method = WebRequestMethods.Http.Get 
Dim response As HttpWebResponse = request.GetResponse() 
Dim reader As New StreamReader(response.GetResponseStream()) 
Dim webpageContents As String = reader.ReadToEnd() 
response.Close() 
+1

現在你已經在頁面的內容,你需要[解析它(http://stackoverflow.com/questions/516811/how-do-you-parse-an- HTML-在-VB網)。在您獲取所需信息後,您可以[將其添加到您的DataTable](https://msdn.microsoft.com/en-us/library/5ycd1034.aspx)。 –

回答

3

雖然不是VB.Net這樣使用另一個.Net語言F#和HTML Type Provider,這是FSharp.Data project的一部分,可以通過Nuget獲得。

的HTML類型提供給您鍵入訪問到Visual Studio中的HTML文件,即

// Reference the FSharp.Data Nuget package 
#r @".\packages\FSharp.Data.2.2.3\lib\net40\FSharp.Data.dll" 
// Type provider over your HTML document specified in yourUrl 
type html = FSharp.Data.HtmlProvider<yourUrl> 
// Get the rows from the HTML table in the page 
let allRows = html.GetSample().Tables.Table1.Rows |> Seq.skip 1 
// Skip empty rows 
let validRows = allRows |> Seq.where (fun row -> row.Name <> "") 

那麼有效行加載到一個DataTable:

// Reference the System.Data assembly 
#r "System.Data.dll" 
// Create a DataTable 
let table = new System.Data.DataTable() 
// Add column names to the table 
for name in ["Parent";"Name";"Last modified";"Size"] do table.Columns.Add(name) |> ignore 
// Add row values to the table 
for row in validRows do 
    table.Rows.Add(row.Column1, row.Name, row.``Last modified``, row.Size) |> ignore 

,最後顯示在一個DataTable中形式:

// Reference the Windows.Forms assembly 
#r "System.Windows.Forms.dll" 
open System.Windows.Forms 
// Create a form 
let form = new Form(Width=480,Height=320) 
// Initialise a grid 
let grid = new DataGridView(Dock=DockStyle.Fill) 
form.Controls.Add(grid) 
// Set the grid data source with the table 
form.Load.Add(fun _ -> grid.DataSource <- table) 
form.Show() 

它顯示了一個窗體中填充的DataGrid:

DataTable

+0

你的男人做到了! – asukagr