SimpleBrowser 浏览器引擎开源项目

我要开发同款
匿名用户2016年07月27日
242阅读

技术信息

开源地址
https://github.com/SimpleBrowserDotNet/SimpleBrowser
授权协议
BSD

作品详情

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;    }}

功能介绍

SimpleBrowser是专门为自动化任务而设计的一个灵活而直观的浏览器引擎,内置.Net 4 framework。 示例代码: class Program
{
    static void...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论