昨晚当我问起屏幕抓取时,我获得了一篇很好的文章链接,并且让我注意到了这一点。然而,我有几个问题。我将发布我的代码以及下面的html源代码。我试图抓住数据表之间的数据,然后将数据发送到一个sql表。我已经找到了成功抓取描述部件3.5 ect ...最后修改由乔然而,因为第1 2/tr还包含img src =/......“alt =”00721408“数字不抓住。我坚持要如何改变代码,以便抓取表中的所有数据。第二,为了准备将数据发送到sql表,我需要做什么。我的代码如下:HTMLAgility帮助屏幕抓取
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Windows.Forms;
namespace ConsoleApplication1
{
}
class Program
{
static void Main(string[] args)
{
// Load the html document
var webGet = new HtmlWeb();
var doc = webGet.Load("http://localhost");
// Get all tables in the document
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
// Iterate all rows in the first table
HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");
for (int i = 0; i < rows.Count; ++i)
{
// Iterate all columns in this row
HtmlNodeCollection cols = rows[i].SelectNodes(".//td");
for (int j = 0; j < cols.Count; ++j)
{
// Get the value of the column and print it
string value = cols[j].InnerText;
Console.WriteLine(value);
}
}
}
}
<table class="data">
<tr><td>Part-Num</td><td width="50"></td><td><img src="/partcode/number/072140" alt="072140"/></td></tr>
<tr><td>Manu-Number</td><td width="50"></td><td><img src="/partcode/manu/00721408" alt="00721408" /></td></tr>
<tr><td>Description</td><td></td><td>Widget 3.5</td></tr>
<tr><td>Manu-Country</td><td></td><td>United States</td></tr>
<tr><td>Last Modified</td><td></td><td>26 Jan 2011, 8:08 PM</td></tr>
<tr><td>Last Modified By</td><td></td><td>
Manu
</td></tr>
</table>
<p>
</body></html>
谢谢我将使用这个想法 – JRB 2011-03-17 04:35:57