SimpleBrowser是专门为自动化任务而设计的一个灵活而直观的浏览器引擎,内置.Net4framework。
示例代码:
class Program{ static void Mai(strig[] args) { var browser = ew Browser(); try { // log the browser request/respose data to files so we ca iterrogate them i case of a issue with our scrapig browser.RequestLogged += OBrowserRequestLogged; browser.MessageLogged += ew Actio<Browser, strig>(OBrowserMessageLogged); // we'll fake the user aget for websites that alter their cotet for urecogised browsers browser.UserAget = "Mozilla/5.0 (Widows; U; Widows NT 6.1; e-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; // browse to GitHub browser.Navigate("https://github.com/"); if(LastRequestFailed(browser)) retur; // always check the last request i case the page failed to load // click the logi lik ad click it browser.Log("First we eed to log i, so browse to the logi page, fill i the logi details ad submit the form."); var logiLik = browser.Fid("a", FidBy.Text, "Logi"); if(!logiLik.Exists) browser.Log("Ca't fid the logi lik! Perhaps the site is dow for maiteace?"); else { logiLik.Click(); if(LastRequestFailed(browser)) retur; // fill i the form ad click the logi butto - the fields are easy to locate because they have ID attributes browser.Fid("logi_field").Value = "youremail@domai.com"; browser.Fid("password").Value = "yourpassword"; browser.Fid(ElemetType.Butto, "ame", "commit").Click(); if(LastRequestFailed(browser)) retur; // see if the logi succeeded - CotaisText() is very forgivig, so do't worry about whitespace, casig, html tags separatig the text, etc. if(browser.CotaisText("Icorrect logi or password")) { browser.Log("Logi failed!", LogMessageType.Error); } else { // After loggig i, we should check that the page cotais elemets that we recogise if(!browser.CotaisText("Your Repositories")) browser.Log("There was't the usual logi failure message, but the text we ormally expect is't preset o the page"); else { browser.Log("Your News Feed:"); // we ca use simple jquery selectors, though advaced selectors are yet to be implemeted foreach(var item i browser.Select("div.ews .title")) browser.Log("* " + item.Value); } } } } catch(Exceptio ex) { browser.Log(ex.Message, LogMessageType.Error); browser.Log(ex.StackTrace, LogMessageType.StackTrace); } fially { var path = WriteFile("log-" + DateTime.UtcNow.Ticks + ".html", browser.RederHtmlLogFile("SimpleBrowser Sample - Request Log")); Process.Start(path); } } static bool LastRequestFailed(Browser browser) { if(browser.LastWebExceptio != ull) { browser.Log("There was a error loadig the page: " + browser.LastWebExceptio.Message); retur true; } retur false; } static void OBrowserMessageLogged(Browser browser, strig log) { Cosole.WriteLie(log); } static void OBrowserRequestLogged(Browser req, HttpRequestLog log) { Cosole.WriteLie(" -> " + log.Method + " request to " + log.Url); Cosole.WriteLie(" <- Respose status code: " + log.ResposeCode); } static strig WriteFile(strig fileame, strig text) { var dir = ew DirectoryIfo(Path.Combie(AppDomai.CurretDomai.BaseDirectory, "Logs")); if(!dir.Exists) dir.Create(); var path = Path.Combie(dir.FullName, fileame); File.WriteAllText(path, text); retur path; }}









评论