【问题标题】:Screen scraping with HTMLAgility help please请帮助使用 HTMLAgility 进行屏幕抓取
【发布时间】: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


    【解决方案1】:

    我对您要获取的数据有点困惑...

    你可以试试:

    SelectNodes("//td[text()='Description']/../child::*[3]")

    其内部文本应为“Widget 3.5”

    SelectNodes("//td[text()='Manu-Country']/../child::*[3]")

    其内部文本应为“美国”

    等等。等等

    顺便说一句,作为一个无耻的插件,你应该看看:systemhtml.codeplex.com 它是另一个 html 解析器。

    【讨论】:

    • 我会看一下systemhtml。是的,你是对的,我想获取内部文本“widget 3.5 United States ect 并将它们发送到一个带有字段 Description、Manu-Country 等的 sql 表。
    【解决方案2】:

    虽然像这样脆弱的东西在你的情况下会起作用 - 基本上只包括所有图像 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2013-12-31
      • 2013-01-12
      • 2017-02-25
      • 2023-03-25
      相关资源
      最近更新 更多