2012-08-03 56 views
0

使用C#或VB.Net代碼從鏈接下載(http://24.173.220.131/carter/currentinmates.aspx)。然後將頁面中的屬性解析爲文本文檔。從aspx頁面解析爲文本文檔

輸出:

名稱| BookDate |收費|保釋|發佈|代理 ANDERSON,JAYME雷蒙| 05/04/2012 |被判刑| $ 0.00包裝| 2022年5月2日|車削SELF IN ANDERSON,JEFFERY CONARD | 02/06/2012 | SENTENCED | $ 0.00 | 2/5/2022 | CARTER COUNTY SHERIFF DEPT

回答

0

使用WebClient class它正是你要找的。

Public Class Test 

Public Shared Sub Main(args() As String) 
    Dim sURL as String 
    If args Is Nothing OrElse args.Length = 0 Then 
     'Throw New ApplicationException("Specify the URI of the resource to retrieve.") 
     sURL = http://24.173.220.131/carter/currentinmates.aspx" 
    Else 
     sURL = args(0) 
    End If 
    Dim client As New WebClient() 

    ' Add a user agent header in case the 
    ' requested URI contains a query. 
    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 

    Dim data As Stream = client.OpenRead(sURL) 
    Dim reader As New StreamReader(data) 
    Dim s As String = reader.ReadToEnd() 
    Console.WriteLine(s) 

    'Here write the variable `s` to a Text file, eg My.File.Create(s) 

    data.Close() 
    reader.Close() 
End Sub 'Main 
End Class 'Test 
2

增加提及CsQuery中的NuGet安裝,或在這裏找到它https://github.com/jamietre/CsQuery

using System; 
using System.Collections.Generic; 
using System.Collections.Concurrent; 
using System.Diagnostics; 
using System.Linq; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Text; 
using CsQuery; 

class Program 
{ 
    static void Main(string[] args) 
    { 

     var stringBuilder = new StringBuilder(); 
     var url = "http://24.173.220.131/carter/currentinmates.aspx"; 
     CQ.CreateFromUrlAsync(url) 
      .Then(response => 
      { 
       var dom = response.Dom; 
       var trs = dom.Select("#dgrdLandRecords tr").Elements; 
       foreach (var row in trs) 
       { 
        stringBuilder.AppendLine(); 
        var tds = row.ChildElements.ToList(); 

        for (int i = 1; i < tds.Count; i++) 
        { 
         stringBuilder.Append(tds[i].Cq().Text()); 
         stringBuilder.Append("|"); 
        } 
       } 
       var result = stringBuilder.ToString(); 
       Console.Write(result); 
      }); 


     Console.WriteLine("Press any key to exit."); 
     Console.ReadKey(); 
    } 
} 
+0

不知道CsQuery ...感謝鏈接:) – harag 2012-08-03 09:14:58

+0

非常感謝查理! – user1572855 2012-08-03 18:00:44

+0

@ user1572855如果解決了您的問題,請考慮接受此答案。 – 2013-03-26 22:23:52