axWebBrowser1使用问题
请高人帮忙看下private void button2_Click(object sender, EventArgs e)
{
String url = "http://www.skyunion.net/book.php";
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
axWebBrowser1.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
}
private void button3_Click(object sender, EventArgs e)
{
IHTMLDocument2 HTMLDocument = (IHTMLDocument2)axWebBrowser1.Document;
string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
string[] arHtml = strHtml.Split('\n');
writeFile("out.txt", strHtml);
}
out.txt的内容并不是http://www.skyunion.net/book.php这个地址的内容,而是上个页面的内容,请大家帮忙看下是什么原因 --------------------编程问答-------------------- 为什么不这样直接取指定网页的内容?
System.Net.WebClient wc = new System.Net.WebClient();--------------------编程问答-------------------- 是不是访问http://www.skyunion.net/book.php后,并没有触发DownloadComplete 事件,所以文本文件的内容是上一次页面(有触发DownloadComplete 事件)的内容? --------------------编程问答-------------------- 我跟踪过了,有触发触发DownloadComplete 事件 --------------------编程问答-------------------- 跟踪看看文档加载完毕没
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
有状态的
还有当前WebBrowser的uri是不是http://www.skyunion.net/book.php --------------------编程问答-------------------- 谢谢楼上的几们兄弟
当前WebBrowser的uri已经跳转到了http://www.skyunion.net/book.php页面 --------------------编程问答-------------------- IHTMLDocument2 HTMLDocument = (IHTMLDocument2)axWebBrowser1.Document;
string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
设置断点看下strHtml的值 --------------------编程问答--------------------
什么原因? --------------------编程问答-------------------- 还没有找到原因 --------------------编程问答-------------------- 要执行两次才能取到我要跳转的那个页面的代码 --------------------编程问答-------------------- System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
这个方法,如果("网页地址")需要验证,则无法取到该网页的内容啊 --------------------编程问答-------------------- axWebBrowser1.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
这2句换个位置看下呢 --------------------编程问答-------------------- 谢谢smg19831002
换下位子不对啊
我读取网页内容的语句写在this.button3_Click中
如果换了顺序完全不对了 --------------------编程问答-------------------- 这样吧,我觉得你的程序应该不是很大,你把代码发出来看下呢 --------------------编程问答-------------------- 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 mshtml;
using System.IO;
namespace GetWebHtml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static public void writeFile(string path, string str)
{
//如果文件path存在就打开,不存在就新建 .append 是追加写, CreateNew 是覆盖
FileStream fst = new FileStream(path, FileMode.CreateNew);
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(str);
swt.Close();
fst.Close();
}
private void button1_Click(object sender, EventArgs e)
{
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
Cursor.Current = Cursors.WaitCursor;
axWebBrowser1.Navigate(textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
Cursor.Current = Cursors.Default;
}
private void button2_Click(object sender, EventArgs e)
{
String url = "http://fengchao.baidu.com/fc-mc/spread/preview.do?userid=1851750&keyword=%E7%BE%8E%E5%AE%B9&area=2";
//String url = "http://www.skyunion.net/book.php";
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
//Cursor.Current = Cursors.WaitCursor;
axWebBrowser1.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
//Cursor.Current = Cursors.Default;
this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
}
private void button3_Click(object sender, EventArgs e)
{
IHTMLDocument2 HTMLDocument = (IHTMLDocument2)axWebBrowser1.Document;
string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
string[] arHtml = strHtml.Split('\n');
writeFile("out.txt", strHtml);
}
private void button4_Click(object sender, EventArgs e)
{
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("http://www.skyunion.net/");
string s = System.Text.Encoding.UTF8.GetString(pageData);
writeFile("out.txt", s);
}
}
}
--------------------编程问答-------------------- 是很简单,我用axWebBrowser做了个最简单的浏览器,
我想通过这个去访问一个页面,然后把那个页面的内容保存下来
要保存的那个页面需要验证 --------------------编程问答-------------------- 我用的2005开发的,没有这个事件
你的axWebBrowser1.DownloadComplete应该是失败了
你在button3_Click里面调试下他的状态看看
--------------------编程问答-------------------- 是用2008开发的 --------------------编程问答-------------------- 我分开两步做就可以的
1.webBrowser1.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr); 先导航到需要保存的那个页面
2. IHTMLDocument2 HTMLDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
string[] arHtml = strHtml.Split('\n');
writeFile("out.txt", strHtml);
再来保存需要保存的内容
用两个按钮来做可以
并到一个里面读出来就是错的 --------------------编程问答-------------------- 已解决,谢谢各位!
把
this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
改成
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.button3_Click);
即可
应该是DocumentCompleted 事件,而不是DownloadComplete 事件
再次谢谢各位! --------------------编程问答-------------------- --------------------编程问答-------------------- 顺便说一下,DownloadCompleted这个事件是可以多次触发的,比如你用webbrowser控件去访问雅虎新闻,然后在DocumentCompleted事件里用自定义的dosth()函数来处理,动态跟踪时有可能处理到一半,又跳到DocumentCompleted事件里了,需要用多线程加上ManualResetEvent来阻塞 --------------------编程问答-------------------- 写错了,应该是:DocumentCompleted这个事件是可以多次触发的
补充:.NET技术 , C#