文件名称:
浏览器引擎SimpleBrowser.zip
开发工具:
文件大小: 623kb
下载次数: 0
上传时间: 2019-07-17
详细说明: SimpleBrowser是专门为自动化任务而设计的一个灵活而直观的浏览器引擎,内置.Net 4 framework。示例代码:class Program { static void Main(string[] args) { var browser = new Browser(); try { // log the browser request/response data to files so we can interrogate them in case of an issue with our scraping browser.Reques tLogged = OnBrowserRequestLogged; browser.MessageLogged = new Action(OnBrowserMessageLogged); // well fake the user agent for websites that alter their content for unrecognised browsers browser.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; // browse to GitHub browser.Navigate("http://github.com/"); if(LastRequestFailed(browser)) return; // always check the last request in case the page failed to load // click the login link and click it browser.Log("First we need to log in, so browse to the login page, fill in the login details and submit the form."); var loginLink = browser.Find("a", FindBy.Text, "Login"); if(!loginLink.Exists) browser.Log("Cant find the login link! Perhaps the site is down for maintenance?"); else { loginLink.Click(); if(LastRequestFailed(browser)) return; // fill in the form and click the login button - the fields are easy to locate because they have ID attributes browser.Find("login_field").Value = "youremaildomain.com"; browser.Find("password").Value = "yourpassword"; browser.Find(ElementType.Button, "name", "commit").Click(); if(LastRequestFailed(browser)) return; // see if the login succeeded - ContainsText() is very forgiving, so dont worry about whitespace, casing, html tags separating the text, etc. if(browser.ContainsText("Incorrect login or password")) { browser.Log("Login failed!", LogMessageType.Error); } else { // After logging in, we should check that the page contains elements that we recognise if(!browser.ContainsText("Your Repositories")) browser.Log("There wasnt the usual login failure message, but the text we normally expect isnt present on the page"); else { browser.Log("Your News Feed:"); // we can use simple jquery selectors, though advanced selectors are yet to be implemented foreach(var item in browser.Select("div.news .title")) browser.Log("* " item.Value); } } } } catch(Exception ex) { browser.Log(ex.Message, LogMessageType.Error); browser.Log(ex.StackTrace, LogMessageType.StackTrace); } finally { var path = WriteFile("log-" DateTime.UtcNow.Ticks ".html", browser.RenderHtmlLogFile("SimpleBrowser Sample - Request Log")); Process.Start(path); } } static bool LastRequestFailed(Browser browser) { if(browser.LastWebException != null) { browser.Log("There was an error loading the page: " browser.LastWebException.Message); return true; } return false; } static void OnBrowserMessageLogged(Browser browser, string log) { Console.WriteLine(log); } static void OnBrowserRequestLogged(Browser req, HttpRequestLog log) { Console.WriteLine(" -> " log.Method " request to " log.Url); Console.WriteLine(" <- Response status code: " log.ResponseCode); } static string WriteFile(string filename, string text) { var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs")); if(!dir.Exists) dir.Create(); var path = Path.Combine(dir.FullName, filename); File.WriteAllText(path, text); return path; } }
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
相关搜索: