解析htmlagilitypack(没有id的表)vb.net

时间:2013-03-20 18:44:51

标签: html vb.net xpath html-table html-agility-pack

希望得到你的答案。

我使用vb.net和htmlagilitypack来获取数据并且它可以工作,但不是我希望它的方式=)

我有这个html页面(部分):


<TABLE WITH=100% BORDER=4>

<TR>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=Lok" >Lok</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=Avg" >Avg&aring;r</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=AvgS" >Station</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=Ank" >Ankommer</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=AnkS" >Station</A></TH>
<TH>Tjänstetyp</TH>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1176&val=Visa+Lokindivid&Bek=Visa">R1176</a></TD>
<TD>Mar-20-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-20-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1267&val=Visa+Lokindivid&Bek=Visa">R1267</a></TD>
<TD>Mar-20-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-20-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1267&val=Visa+Lokindivid&Bek=Visa">R1267</a></TD>
<TD>Mar-20-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-20-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-21-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-21-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-21-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-21-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=RXXXXX&val=Visa+Lokindivid&Bek=Visa">RXXXXX</a></TD>
<TD>Mar-21-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-21-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>B1\B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-25-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-25-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-25-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-25-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1254&val=Visa+Lokindivid&Bek=Visa">R1254</a></TD>
<TD>Mar-27-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-27-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=RXXXXX&val=Visa+Lokindivid&Bek=Visa">RXXXXX</a></TD>
<TD>Mar-27-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-27-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B1\B2</TD>
</TR>
</TABLE>
<A><A>Senast uppdaterad: Mar-20-2013 18:16:00</A><BR>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="20%" bgcolor="#009900"  align="left">
<IMG src="http://litmgc101.greencargo.com/bottenbild.jpg" alt="Green Cargo" width=800 height=25 border=0>
</TD>
</TR>
<TR>
</table>

我想要做的是使用(例如)“R1176”和日期“Mar-20-2013 13:04:00”获取部件。 (宁愿没有时间“13:04:00”),但如果我不能在解析阶段跳过它,我可以在VB.net中删除它。

所以简单地解释一下我想要做的是: 获取所有“R1234”及其随附的日期,然后将其放入“R4321”的文本框和日期或其他内容的另一个文本框中。

1 个答案:

答案 0 :(得分:0)

在C#中我会做这样的事情:

var result =
    doc.DocumentNode.SelectNodes("//td/a[contains(@href,'Lokindivid')]")
       .Select(node => new KeyValuePair<string, DateTime>(node.InnerText, DateTime.Parse(node.SelectSingleNode("./ancestor::tr[1]/td[2]").InnerText).Date));

我的VB.NET foo产生了以下代码(这是一个字面翻译),它与您提供的示例html一起使用:

Dim doc As New HtmlDocument
doc.LoadHtml(Content.Html)

Dim items = doc.DocumentNode.SelectNodes("//td/a[contains(@href,'Lokindivid')]").Select(Function(node) New KeyValuePair(Of String, DateTime)(node.InnerText, DateTime.Parse(node.SelectSingleNode("./ancestor::tr[1]/td[2]").InnerText).Date))

For Each item As KeyValuePair(Of String, Date) In items
    Console.WriteLine(item.Key)
    Console.WriteLine(item.Value)
Next