使用htmlagilitypack在c#中获取html表

时间:2017-03-27 16:08:44

标签: c# datagridview html-agility-pack

我开始使用c#,我试图从网站上获取一个表并在datagridview中显示它。 我试着先获得前两列,我可以得到数据,但它显示在第二列。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using HtmlAgilityPack;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        private DataTable dt;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string htmlCode = "";
            using (WebClient client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.UserAgent, "AvoidError");
                htmlCode = client.DownloadString("http://www.sismologia.cl/links/ultimos_sismos.html");
            }
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(htmlCode);

            dt = new DataTable();
            dt.Columns.Add("Local", typeof(string));
            dt.Columns.Add("UTC", typeof(string));

            int count = 0;

            foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
            {
                richTextBox1.Text = htmlCode;

                foreach (HtmlNode row in table.SelectNodes("//*[@id=\"main\"]/table//tr"))
                {
                    foreach (var cell in row.SelectNodes("//*[@id=\"main\"]/table//tr//td"))
                    {
                        DataRow dr = dt.NewRow();
                        if ((count % 2 == 0))
                        {
                            dr["Local"] = cell.InnerText.Replace(" ", " ");
                        }
                        else
                        {
                            dr["UTC"] = cell.InnerText.Replace(" ", " ");

                            dt.Rows.Add(dr);
                        }
                        count++;
                    }
                }

                dataGridView1.DataSource = dt;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案,直接选择tr div中的所有main并循环显示:

        string htmlCode = "";
        using (WebClient client = new WebClient())
        {
            client.Headers.Add(HttpRequestHeader.UserAgent, "AvoidError");
            htmlCode = client.DownloadString("http://www.sismologia.cl/links/ultimos_sismos.html");
        }
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

        doc.LoadHtml(htmlCode);

        var dt = new DataTable();
        dt.Columns.Add("Local", typeof(string));
        dt.Columns.Add("UTC", typeof(string));

        foreach (var row in doc.DocumentNode.SelectNodes("//*[@id='main']//tr"))
        {
            var nodes = row.SelectNodes("td");
            if (nodes != null)
            {

                var local = nodes[0].InnerText;
                var utc = nodes[1].InnerText;

                dt.Rows.Add(local, utc);
            }
        }