本人在制作抓取网页内容的小工具,目前可抓大部分网站,通过HttpWebRequest模拟浏览器抓取内容,但发现很多站做了屏蔽,直接用HttpWebRequest不能抓的内容,发现用webbrouser可以获取任意网站页面的内容,我的问题是:
1:如何将webbrouser写到一个函数里直接调用?2:或者如何基于ie或firefox的浏览器,通过调用他们会的网页源代码?
1:如何将webbrouser写到一个函数里直接调用?2:或者如何基于ie或firefox的浏览器,通过调用他们会的网页源代码?
经试验,webbrouser控件可以轻易获得任意网站源代码,发现基于浏览器访问的方式就可以办到,不知道如何webbrouser控件获取html源代码封装到一个函数中,发现很难办到
发现Webbrowser属性设为隐藏,将不能正确获得网页源代码了。如果你可以,麻烦提供详细的源代码
public static string fetchHtmlStr3(string url, string Encodingstr)
{ WebBrowser webBrowser1 = new WebBrowser();
//webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{ } StreamReader sr = new StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding(Encodingstr));
html = sr.ReadToEnd();
return html;
}
不考虑跳转的问题我的问题解决方法:
1.webbrouser ,如何封装成函数调用,我不需要webbrouser控件显示,webbrouser控件动态生成,获得html页面后就注销,我写的函数参照上面,不知道问题出在哪?2.如何哪位兄弟知道如何调用ie,或firefox浏览器来获得网页内容,这个也可以
正确回答上面任意一个问题,满足要求后立即结贴,谢谢!
http://topic.csdn.net/u/20090417/11/8b44f86d-7892-439d-ab95-f182a43a1d24.html
ping通的话,说明是包过滤,这是目前网站屏蔽的大多数措施,通过屏蔽网站域名或者关键字,IP不屏蔽;
ping不通的话,你得考虑代理了,不然最基本的TCP连接都无法建立也就别提了。我抓包分析过大致穿墙的工具使用的原理,一种是通过服务器中转,另一种是通过HTTP头文件信息的修改。
既然楼主是想用C#来抓取,完全可以自由控制HTTP的头文件信息的,没必要用默认的,默认的信息往往是给屏蔽的要素。比如你在HTTP头信息里将访问的url地址改为别的,但是却发送给正确的url地址会如何?请求会顺利到达正确的url地址而检查头信息却不一致,而一般网站是不会验证头信息的,头信息往往是给屏蔽软件用来屏蔽的一个途径。——点到为止,就说这些了。
的确如兄弟所说,webbrouser ,如何封装成函数调用,是线程同步问题,,要等到整个过程跑完后才执行,不知如何解决,我需要直接调用这个函数就能获得html源文件。public static string fetchHtmlStr3(string url, string Encodingstr)
{ WebBrowser webBrowser1 = new WebBrowser();
//webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{ } StreamReader sr = new StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding(Encodingstr));
html = sr.ReadToEnd();
return html;
}
这个就是我需要的,不过有个问题,我下来调试的时候不成功,
在执行IE ie = new IE("http://www.google.com")
总是跳出错误,不知是怎么问题
Error HRESULT E_FAIL has been returned from a call to a COM component.
http://topic.csdn.net/u/20090507/14/0f73d22e-c726-4dfa-addd-d5bb4cc614ff.htmlhttp://topic.csdn.net/u/20090507/13/98c41f43-aab3-438d-a105-9c94c6ce6cb9.html
能分享下成功不!email :[email protected]
[email protected]