【发布时间】:2011-07-17 01:51:32
【问题描述】:
昨晚当我询问有关屏幕抓取的问题时,我得到了一个很好的文章链接,并且让我明白了这一点。不过我有几个问题。我将在下面发布我的代码以及 html 源代码。我正在尝试抓取数据表之间的数据,然后将数据发送到 sql 表。我在获取 Description Widget 3.5 等方面取得了成功...但是最后由 Joe 修改,因为 1st 2 /tr 还包含 img src=/......" alt="00721408" 数字没有被抓取。我我被困在如何更改代码以便抓取表中的所有数据。第二,接下来我需要做什么才能准备将数据发送到 sql 表。我的代码如下:
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>
【问题讨论】:
标签: c# .net sql database web-scraping