streamreader读取每一行,然后按空格分割,可以判断第一个字是否为“豆”,来区分有意义的行,分割的数组,自己添加到row对应的cell中就行了

解决方案 »

  1.   

    下载开源类HtmlAgilityPack,下面的代码解析成List,需要的话自己转成DataSetusing System;
    using System.IO;
    using System.Net;
    using System.Collections.Generic;
    using HtmlAgilityPack;namespace dce
    {
    class Program
    {
    public static void Main(string[] args)
    {
    System.Diagnostics.Debug.Print(trPath);
    IList<string[]> ds = Extract();
    for (int i = 0; i < ds.Count; ++i)
    {
    for (int j = 0; j < ds[i].Length; ++j)
    {
    Console.Write(string.Format("{0}\t", ds[i][j].ToString()));
    }
    Console.Write("\r\n");
    }
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }

    const string url = @"http://www.dce.com.cn/PublicWeb/MainServlet?action=Pu00011_result&Pu00011_Input.trade_date=20140827&Pu00011_Input.variety=all&Pu00011_Input.trade_type=0";
    const string tablePath = "//table[contains(@class, 'table')]";
    const string subTotalPath = ".//td[1][(contains(text(),\"小计\"))]";
    const string totalPath = ".//td[1][(contains(text(),\"总计\"))]";
    static string trPath = "./tr[not(" + subTotalPath + "|" + totalPath +")]";
    static string tdPath = "./td";

    public static IList<string[]> Extract()
    {
    IList<string[]> list = new List<string[]>();
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    using (WebClient webClient = new WebClient())
    {
    using (Stream stream = webClient.OpenRead(url))
    {
    doc.Load(stream);
    }
    }

    HtmlNode root = doc.DocumentNode;
    HtmlNode node = root.SelectSingleNode(tablePath);
    if (null != node)
    {
    HtmlNodeCollection trs = node.SelectNodes(trPath);
    if (null != trs)
    {
    for (int i = 1; i < trs.Count; ++i)
    {
    HtmlNodeCollection tds = trs[i].SelectNodes(tdPath);
    if (null != tds)
    {
    string[] row = new string[tds.Count];
    for (int j = 0; j < tds.Count; ++j)
    {
    row[j] = tds[j].InnerText;
    }
    list.Add(row);
    }
    }
    }
    }

    return list;
    }
    }
    }
      

  2.   


    非常感谢 我正在用webBrowser来做 您这个方法挺容易的 我试试  
      

  3.   

    实际上,你看URL地址,自己构造一个GET Form去取回数据MainServlet?action=Pu00011_result&Pu00011_Input.trade_date=20140827&Pu00011_Input.variety=all&Pu00011_Input.trade_type=0
      

  4.   

    不客气,很高兴能有帮助
    因为你访问的页面不需要登陆,而且是Get方式访问的,所以如楼上说的,你可以自己查看一下网页的源码,就可以构造好相应的网址,比如把Pu00011_Input.trade_date=20140827中的日期换成其他需要的日期,就可以获取相应页面的信息了。
    如果对给出的回复满意,请结贴;如果还有其他问题,请回帖提出。
      

  5.   

    其实你使用WebBrowser,也可以自己写一段js,然后调用它也可以提取出数据,下面的代码只是也是,没有太多的空值判断,仅供参考using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;namespace dceWB
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public class MainForm : Form
    {
    /// Designer variable used to keep track of non-visual components.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Disposes resources used by the form.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
    if (disposing) {
    if (components != null) {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    /// <summary>
    /// This method is required for Windows Forms designer support.
    /// Do not change the method contents inside the source code editor. The Forms designer might
    /// not be able to load this method if it was changed manually.
    /// </summary>
    private void InitializeComponent()
    {
    this.webBrowser1 = new System.Windows.Forms.WebBrowser();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // webBrowser1
    // 
    this.webBrowser1.Location = new System.Drawing.Point(1, 39);
    this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
    this.webBrowser1.Name = "webBrowser1";
    this.webBrowser1.Size = new System.Drawing.Size(782, 250);
    this.webBrowser1.TabIndex = 0;
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(13, 4);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(593, 21);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "http://www.dce.com.cn/PublicWeb/MainServlet?action=Pu00011_result&Pu00011_Input.t" +
    "rade_date=20140827&Pu00011_Input.variety=all&Pu00011_Input.trade_type=0";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(690, 4);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 2;
    this.button1.Text = "提取数据";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.Button1Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(612, 4);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(75, 23);
    this.button2.TabIndex = 3;
    this.button2.Text = "打开网页";
    this.button2.UseVisualStyleBackColor = true;
    this.button2.Click += new System.EventHandler(this.Button2Click);
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(1, 296);
    this.textBox2.Multiline = true;
    this.textBox2.Name = "textBox2";
    this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
    this.textBox2.Size = new System.Drawing.Size(782, 254);
    this.textBox2.TabIndex = 4;
    // 
    // MainForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(784, 562);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.webBrowser1);
    this.Name = "MainForm";
    this.Text = "bceWB";
    this.Load += new System.EventHandler(this.MainFormLoad);
    this.ResumeLayout(false);
    this.PerformLayout();
    }
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.WebBrowser webBrowser1;

    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }

    void MainFormLoad(object sender, EventArgs e)
    {
    this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser1DocumentCompleted);
    this.webBrowser1.Navigate(this.textBox1.Text);
    }

    void WebBrowser1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    this.button1.Enabled = true;
    }

    void Button2Click(object sender, EventArgs e)
    {
    this.webBrowser1.Navigate(this.textBox1.Text);
    }

    void Button1Click(object sender, EventArgs e)
    {
    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
    {
    string scriptstr = @"GetCellValues();
    function GetCellValues() {
    var ret = '';
        var table = document.getElementsByTagName('table')[1];
        for (var r = 1, n = table.rows.length; r < n; r++) {
            c0 = table.rows[r].cells[0].innerHTML;
            if (c0.indexOf('小计') > -1 || c0.indexOf('总计') > -1) {
                continue;
            }
            for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                 ret += table.rows[r].cells[c].innerHTML + '\t';
            }
            ret = ret.substring(0, ret.length - 1);
            ret += '\r\n';
        }
        return ret;
    }";
    string result = sendJS(webBrowser1, scriptstr);
    textBox2.Text = result;

    //下面代码解析数据为二维数组
    //string[] row = result.Split('\r','\n');
    //for (int i = 0; i < row.Length; ++i)
    //{
    // string[] col = row[i].Split('\t');
    // for (int j = 0; j < col.Length; ++j) {
    // System.Diagnostics.Debug.Print(col[j]);
    // }
    //}
    }
    }

    private string sendJS(WebBrowser sender, string JScript) {
    object[] args = {JScript};
    return sender.Document.InvokeScript("eval",args).ToString();
    }
    }
    }
      

  6.   


    大师 你太厉害了  我试试  马上结贴  能给个联系方式吗  说实话 还有个json的网页 想请教下您