正在从aspx页面解析为文本文档
本文关键字:文本 文档 aspx | 更新日期: 2024-09-25 20:00:24
使用C#或VB.Net代码从链接下载(http://24.173.220.131/carter/currentinmates.aspx)。然后将页面中的属性解析为文本文档。
输出:
姓名|预订日期|收费|保释|释放|代理ANDERSON,JAYME RAMONE | 2012年4月5日|被判刑|0.00美元|2022年5月2日|自首杰弗里·科纳德安德森|2012年6月2日|发送|0.00美元|2022年2月5日|卡特县警长部门
添加对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();
}
}
使用WebClient类正是您想要的。
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