using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace Hexun_futures_data_capture
{
partial class HfBF
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
///
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataBrowser = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// DataBrowser
//
this.DataBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.DataBrowser.Location = new System.Drawing.Point(0, 0);
this.DataBrowser.MinimumSize = new System.Drawing.Size(20, 20);
this.DataBrowser.Name = "DataBrowser";
this.DataBrowser.Size = new System.Drawing.Size(949, 439);
this.DataBrowser.TabIndex = 0;
//
// HfBF
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(949, 439);
this.Controls.Add(this.DataBrowser);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "HfBF";
this.Text = "和讯数据预览";
this.ResumeLayout(false); } #endregion public int Hfi;
public string iUrl;
public string iCode;
public string iPath;
public string[] iSpot;
private System.Windows.Forms.WebBrowser DataBrowser;
public void BrowserThread()
{
//HfCode = comboSymbol.Text.ToString();
//string HfUrl = HfUrlbase + HfCode + Spotmonth1[Hfi] + "&begintime=" + tStartDate.Text + "&endtime=" + tStopDate.Text;
this.DataBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.ProcessDocument);
this.DataBrowser.Navigate(iUrl);
//System.Threading.Thread.Sleep(5000);
} private void ProcessDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter w = new StreamWriter(iPath + "/" + iCode + iSpot[Hfi] + ".txt", false, Encoding.GetEncoding(DataBrowser.Document.Encoding));
HtmlDocument htmldoc = ((WebBrowser)sender).Document;
HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("tr");
for (int i = 3; i < htmlTR.Count; i = i + 2)
{
string HfDate = htmlTR[i].GetElementsByTagName("td")[0].InnerText;
string HfOpen = htmlTR[i].GetElementsByTagName("td")[2].InnerText;
string HfHigh = htmlTR[i].GetElementsByTagName("td")[4].InnerText;
string HfLow = htmlTR[i].GetElementsByTagName("td")[6].InnerText;
string HfClose = htmlTR[i].GetElementsByTagName("td")[8].InnerText;
string HfPriceChance = htmlTR[i].GetElementsByTagName("td")[10].InnerText;
string HfVolume = htmlTR[i].GetElementsByTagName("td")[12].InnerText;
string HfHVolume = htmlTR[i].GetElementsByTagName("td")[14].InnerText;
string HfHVChange = htmlTR[i].GetElementsByTagName("td")[16].InnerText;
w.WriteLine(HfDate + "," + HfOpen + "," + HfHigh + "," + HfLow + "," + HfClose + "," + HfPriceChance + "," + HfVolume + "," + HfHVolume + "," + HfHVChange);
}
w.Close();
}
}
}这是一个自己写的类,包含了WebBrowser组件,用来从和讯网上抓取数据,然后分离出特定的字段并写入一个文件。 private void btnExport_Click(object sender, EventArgs e)
{
HfBF iBrowser = new HfBF();
iBrowser.Visible = true;
for (j = 0; j < 12; j++)
{
HfCode = comboSymbol.Text.ToString();
string HfUrl = HfUrlbase + HfCode + Spotmonth1[j] + "&begintime=" + tStartDate.Text + "&endtime=" + tStopDate.Text;
Thread iCollector = new Thread(new ThreadStart(iBrowser.BrowserThread));
//iCollector.SetApartmentState(ApartmentState.STA);
iBrowser.iCode = HfCode;
iBrowser.iUrl = HfUrl;
iBrowser.Hfi = j;
iBrowser.iSpot = Spotmonth1;
iBrowser.iPath = textPath.Text;
iCollector.Start ();
iCollector .Join ();
iCollector.Abort();
} }按下按钮以后,就开启一个新的线程,然后调用上面哪个类的一个方法,从和讯网查询一个页面,然后等待数据处理完毕,循环查询下一个页面。现在的问题是:只有循环到最后一次的时候,才能触发WebBrowser的.DocumentCompleted事件!!! 也就是说一系列的网站查询都没问题,但是数据分析却只有最后一次成功并写入了本地txt文件中。实在搞得我头大了,请各位大侠帮我看看,到底是怎么回事! 先谢谢了
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace Hexun_futures_data_capture
{
partial class HfBF
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
///
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataBrowser = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// DataBrowser
//
this.DataBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.DataBrowser.Location = new System.Drawing.Point(0, 0);
this.DataBrowser.MinimumSize = new System.Drawing.Size(20, 20);
this.DataBrowser.Name = "DataBrowser";
this.DataBrowser.Size = new System.Drawing.Size(949, 439);
this.DataBrowser.TabIndex = 0;
//
// HfBF
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(949, 439);
this.Controls.Add(this.DataBrowser);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "HfBF";
this.Text = "和讯数据预览";
this.ResumeLayout(false); } #endregion public int Hfi;
public string iUrl;
public string iCode;
public string iPath;
public string[] iSpot;
private System.Windows.Forms.WebBrowser DataBrowser;
public void BrowserThread()
{
//HfCode = comboSymbol.Text.ToString();
//string HfUrl = HfUrlbase + HfCode + Spotmonth1[Hfi] + "&begintime=" + tStartDate.Text + "&endtime=" + tStopDate.Text;
this.DataBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.ProcessDocument);
this.DataBrowser.Navigate(iUrl);
//System.Threading.Thread.Sleep(5000);
} private void ProcessDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter w = new StreamWriter(iPath + "/" + iCode + iSpot[Hfi] + ".txt", false, Encoding.GetEncoding(DataBrowser.Document.Encoding));
HtmlDocument htmldoc = ((WebBrowser)sender).Document;
HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("tr");
for (int i = 3; i < htmlTR.Count; i = i + 2)
{
string HfDate = htmlTR[i].GetElementsByTagName("td")[0].InnerText;
string HfOpen = htmlTR[i].GetElementsByTagName("td")[2].InnerText;
string HfHigh = htmlTR[i].GetElementsByTagName("td")[4].InnerText;
string HfLow = htmlTR[i].GetElementsByTagName("td")[6].InnerText;
string HfClose = htmlTR[i].GetElementsByTagName("td")[8].InnerText;
string HfPriceChance = htmlTR[i].GetElementsByTagName("td")[10].InnerText;
string HfVolume = htmlTR[i].GetElementsByTagName("td")[12].InnerText;
string HfHVolume = htmlTR[i].GetElementsByTagName("td")[14].InnerText;
string HfHVChange = htmlTR[i].GetElementsByTagName("td")[16].InnerText;
w.WriteLine(HfDate + "," + HfOpen + "," + HfHigh + "," + HfLow + "," + HfClose + "," + HfPriceChance + "," + HfVolume + "," + HfHVolume + "," + HfHVChange);
}
w.Close();
}
}
}这是一个自己写的类,包含了WebBrowser组件,用来从和讯网上抓取数据,然后分离出特定的字段并写入一个文件。 private void btnExport_Click(object sender, EventArgs e)
{
HfBF iBrowser = new HfBF();
iBrowser.Visible = true;
for (j = 0; j < 12; j++)
{
HfCode = comboSymbol.Text.ToString();
string HfUrl = HfUrlbase + HfCode + Spotmonth1[j] + "&begintime=" + tStartDate.Text + "&endtime=" + tStopDate.Text;
Thread iCollector = new Thread(new ThreadStart(iBrowser.BrowserThread));
//iCollector.SetApartmentState(ApartmentState.STA);
iBrowser.iCode = HfCode;
iBrowser.iUrl = HfUrl;
iBrowser.Hfi = j;
iBrowser.iSpot = Spotmonth1;
iBrowser.iPath = textPath.Text;
iCollector.Start ();
iCollector .Join ();
iCollector.Abort();
} }按下按钮以后,就开启一个新的线程,然后调用上面哪个类的一个方法,从和讯网查询一个页面,然后等待数据处理完毕,循环查询下一个页面。现在的问题是:只有循环到最后一次的时候,才能触发WebBrowser的.DocumentCompleted事件!!! 也就是说一系列的网站查询都没问题,但是数据分析却只有最后一次成功并写入了本地txt文件中。实在搞得我头大了,请各位大侠帮我看看,到底是怎么回事! 先谢谢了
本来我也想这样的,但是WebBrowser组件必须在Form里面才能很好的运行,如果离开了Form单独来获取数据,总是各种错误。 所以暂时就运行一个线程,一个个的来。开太多全屏幕全是窗口了,呵呵。
在MSDN英文版里面,也有一个人遇到类似的问题,没有得到回复。
private void btnExport_Click(object sender, EventArgs e)
{
MyThread = null;
MyThread = new Thread[12];
for (int iTh = 0; iTh < MyThread.Length; iTh++)
{
MyThread[iTh] = null;
MyThread[iTh] = new Thread(new ThreadStart(iBrowser.BrowserThread));
MyThread[iTh].IsBackground = true;
MyThread[iTh].Name = "MyThread" + iTh.ToString();
MyThread[iTh].Start();
}
foreach (Thread thread in MyThread)
{
if (thread != null)
thread.Join();
}
}
经过测试,问题照旧! 搜索google也没有任何进展。
在csdn也发现另外一个哥们有这个问题,当WebBrowser只打开一个页面的时候,似乎不会出问题,一旦涉及到多线程和多个页面的时候,总是得不到正确的数据。
实在撑不住了,睡觉了。