C# WebBrowser 编程 使用mshtml读取网页内容
2014-04-25 15:38
543 查看
首先需要引用Microsoft.mshtml,AxInterop.SHDocVw
一些方法:
webBrowser.ScriptErrorsSuppressed = true;//允许脚本调试
webBrowser.WebBrowserShortcutsEnabled = true;//允许快捷键
/*********************一些事件的处理*******************/
webBrowser.CanGoForwardChanged += new EventHandler(webBrowser_CanGoForwardChanged);
webBrowser.CanGoBackChanged += new EventHandler(webBrowser_CanGoBackChanged);
webBrowser.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
webBrowser.DocumentTitleChanged += new EventHandler(webBrowser1_DocumentTitleChanged);
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser.Disposed += new EventHandler(webBrowser1_Disposed);
(webBrowser.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(FrmIEExplorer_NewWindow3);
webBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
/*********************************************************/
private WebBrowser CreateNewWebBrowser()
{
WebBrowser _axWebBrowser = new WebBrowser();
_axWebBrowser.Tag = new HE_WebBrowserTag();
_axWebBrowser.Dock = DockStyle.Fill;
TabPage _TabPage = new TabPage();
_TabPage.Controls.Add(_axWebBrowser);
RegisterEvent(_axWebBrowser);
flatTabControl1.TabPages.Add(_TabPage);
flatTabControl1.SelectedTab = _TabPage;
_ActiveWebBrowser = _axWebBrowser;
btn_Remove.Enabled = flatTabControl1.TabPages.Count >= 1;
// tblCloseTab.Enabled = true;
return _axWebBrowser;
}
/// <summary>
/// 新窗口事件
/// </summary>
/// <param name="ppDisp"></param>
/// <param name="Cancel"></param>
/// <param name="dwFlags"></param>
/// <param name="bstrUrlContext"></param>
/// <param name="bstrUrl"></param>
void FrmIEExplorer_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
Cancel = true;
ppDisp = (this._ActiveWebBrowser.ActiveXInstance as SHDocVw.WebBrowser).Application;//这里是关键
WebBrowser _axWebBrowser = CreateNewWebBrowser();
_axWebBrowser.Navigate(bstrUrl);
}
//读取网页内容
private void ReadDoc(IHTMLDocument2 doc)
{
int inputcount = doc.all.length;
foreach (IHTMLElement item in doc.all)
{
if (item.id == "_petName" || item.id == "userNameTopLine")
{
this.rich_src.AppendText(" id:");
this.rich_src.AppendText(item.innerText);
}
//this.rich_src.AppendText(" id:");
//this.rich_src.AppendText(item.id == null ? "未设置" : item.id);
//this.rich_src.AppendText(" tag:");
//this.rich_src.AppendText(item.tagName);
//this.rich_src.AppendText(" innerHTML:");
//this.rich_src.AppendText(item.innerHTML == null ? "未设置" : item.innerHTML);
//this.rich_src.AppendText("\r\n-------------\r\n");
if (item.tagName.Equals("FRAME"))
{
//this.rich_src.AppendText(" IFRAME id:");
//this.rich_src.AppendText(item.id == null ? "未设置" : item.id);
//this.rich_src.AppendText("\r\n-------------\r\n");
mshtml.IHTMLFrameBase2 iFrame = (mshtml.IHTMLFrameBase2)item;
mshtml.IHTMLDocument2 iHTMLDocument2 = iFrame.contentWindow.document;
ReadDoc(iHTMLDocument2);
}
}
}
下面2句代码获取IHTMLDocument2
IHTMLDocument2 doc = _ActiveWebBrowser.Document.DomDocument as IHTMLDocument2;
ReadDoc(doc);
一些方法:
webBrowser.ScriptErrorsSuppressed = true;//允许脚本调试
webBrowser.WebBrowserShortcutsEnabled = true;//允许快捷键
/*********************一些事件的处理*******************/
webBrowser.CanGoForwardChanged += new EventHandler(webBrowser_CanGoForwardChanged);
webBrowser.CanGoBackChanged += new EventHandler(webBrowser_CanGoBackChanged);
webBrowser.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
webBrowser.DocumentTitleChanged += new EventHandler(webBrowser1_DocumentTitleChanged);
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser.Disposed += new EventHandler(webBrowser1_Disposed);
(webBrowser.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(FrmIEExplorer_NewWindow3);
webBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
/*********************************************************/
private WebBrowser CreateNewWebBrowser()
{
WebBrowser _axWebBrowser = new WebBrowser();
_axWebBrowser.Tag = new HE_WebBrowserTag();
_axWebBrowser.Dock = DockStyle.Fill;
TabPage _TabPage = new TabPage();
_TabPage.Controls.Add(_axWebBrowser);
RegisterEvent(_axWebBrowser);
flatTabControl1.TabPages.Add(_TabPage);
flatTabControl1.SelectedTab = _TabPage;
_ActiveWebBrowser = _axWebBrowser;
btn_Remove.Enabled = flatTabControl1.TabPages.Count >= 1;
// tblCloseTab.Enabled = true;
return _axWebBrowser;
}
/// <summary>
/// 新窗口事件
/// </summary>
/// <param name="ppDisp"></param>
/// <param name="Cancel"></param>
/// <param name="dwFlags"></param>
/// <param name="bstrUrlContext"></param>
/// <param name="bstrUrl"></param>
void FrmIEExplorer_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
Cancel = true;
ppDisp = (this._ActiveWebBrowser.ActiveXInstance as SHDocVw.WebBrowser).Application;//这里是关键
WebBrowser _axWebBrowser = CreateNewWebBrowser();
_axWebBrowser.Navigate(bstrUrl);
}
//读取网页内容
private void ReadDoc(IHTMLDocument2 doc)
{
int inputcount = doc.all.length;
foreach (IHTMLElement item in doc.all)
{
if (item.id == "_petName" || item.id == "userNameTopLine")
{
this.rich_src.AppendText(" id:");
this.rich_src.AppendText(item.innerText);
}
//this.rich_src.AppendText(" id:");
//this.rich_src.AppendText(item.id == null ? "未设置" : item.id);
//this.rich_src.AppendText(" tag:");
//this.rich_src.AppendText(item.tagName);
//this.rich_src.AppendText(" innerHTML:");
//this.rich_src.AppendText(item.innerHTML == null ? "未设置" : item.innerHTML);
//this.rich_src.AppendText("\r\n-------------\r\n");
if (item.tagName.Equals("FRAME"))
{
//this.rich_src.AppendText(" IFRAME id:");
//this.rich_src.AppendText(item.id == null ? "未设置" : item.id);
//this.rich_src.AppendText("\r\n-------------\r\n");
mshtml.IHTMLFrameBase2 iFrame = (mshtml.IHTMLFrameBase2)item;
mshtml.IHTMLDocument2 iHTMLDocument2 = iFrame.contentWindow.document;
ReadDoc(iHTMLDocument2);
}
}
}
下面2句代码获取IHTMLDocument2
IHTMLDocument2 doc = _ActiveWebBrowser.Document.DomDocument as IHTMLDocument2;
ReadDoc(doc);
相关文章推荐
- C#使用Gzip解压缩完整读取网页内容
- C#使用Gzip解压缩完整读取网页内容
- C#使用GZIP解压缩完整读取网页内容
- 黄聪:C#里如何使用WebBrowser获取处理AJAX生成的网页内容?
- VB使用WebBrowser读取网页内容
- 黄聪:C#里如何使用WebBrowser获取处理AJAX生成的网页内容?
- delphi 使用webbrowser取得网页中表格(Table)内容的正确方法
- 使用CInternetSession和CHttpFile读取网页内容
- C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录
- 使用读写文件读取网页内容
- c#中使用webbrowser在软件中嵌入网页
- 黑马程序员之C#学习笔记:使用Stream.BeginRead方法读取FileStream的流内容
- C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录
- C#正则表达试来读取网页所有内容里的链接地址和链接内容
- C#远程获取/读取网页内容
- 使用Net/C#抓取网页内容
- 1.C#开发winform 开发调运检疫离线制证功能。调用cLodop打印控件(NVelocity模块,生成HTML(包含cLodop打印控件的js),使用WebBrowser,展现网页)总结
- C#使用Webbrowser来判断网页是否加载完毕
- C#读取文件内容,包括ArrayList使用和类型转换
- 如何使用WebBrowser控件打印格式化的XML文档,并以编程方式任意设置打印方向(C#完整示例)