请使用HTMLAgility进行屏幕抓取

时间:2011-03-17 01:42:05

标签: c# .net sql database web-scraping

昨晚,当我询问有关屏幕抓取的问题时,我得到了一篇很好的文章链接,让我明白了这一点。不过我有几个问题。我将发布我的代码以及下面的html源代码。我试图抓取数据表之间的数据,然后将数据发送到sql表。我已经找到成功获取描述小部件3.5等...但最后修改由Joe,因为第1个2 / tr还包含img src = / ......“alt =”00721408“数字不被抓住。我我被困在如何改变代码以便抓取表中的所有数据。第二,为了准备要发送到sql表的数据,我需要做什么。我的代码如下:< / p>

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>

2 个答案:

答案 0 :(得分:0)

虽然像你这样脆弱的东西会适用于你的情况 - 基本上只包括所有图像alt属性的文本内容:

// 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)
    {
        var images = cols[j].SelectNodes("img");
        if(images!=null)
            foreach (var image in images)
            {
                if(image.Attributes["alt"]!=null)
                    Console.WriteLine(image.Attributes["alt"].Value);
            }
        // Get the value of the column and print it
        string value = cols[j].InnerText;
        Console.WriteLine(value);
    }
}

答案 1 :(得分:0)

对于你想要获得的数据而言,我很困惑......

你可以尝试:

的SelectNodes( “// TD [文本()= '描述'] /../子:: * [3]”)

其内部文字应为“Widget 3.5”

的SelectNodes( “// TD [文本()= '马努国'] /../子:: * [3]”)

其内部文字应为“美国”

等。等

顺便说一下,你应该看看:systemhtml.codeplex.com 这是另一个HTML解析器。