public string GetHtml(string url) { using (var outFs = new MemoryStream()) { try { phantomJS.RunScript(@" var system = require('system'); var page = require('webpage').create(); page.open(system.args[1], function() { system.stdout.writeLine(page.content); phantom.exit(); }); " , new string[] { url }, null, outFs); } catch (Exception ex) { Console.WriteLine($"[{nameof(BaseSite)}] - [{nameof(GetHtml)}] -Error getting html from '{url}'. Exception: {ex.Message}"); return(string.Empty); } finally { phantomJS.Abort(); } return(Encoding.UTF8.GetString(outFs.ToArray())); } }
private static string ExtractHtmlWithPhantomJSNoWebdriver(string url) { Logger.Info($"Calling {url} with PhantomJS"); string javascriptQuery = @"var page = require('webpage').create(); page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'; page.onError = function(msg, trace) { //prevent js errors from showing in page.content return; }; page.open('" + url + @"', function (status) { console.log(page.content); //page source phantom.exit(); });"; string text = ""; using (var outputStream = new MemoryStream()) using (var phantom = new PhantomJS()) { phantom.CustomArgs = "--ssl-protocol=any --proxy-type=none"; phantom.RunScript( javascriptQuery, null, null, outputStream ); text = System.Text.Encoding.UTF8.GetString(outputStream.ToArray()); } Logger.Debug($"Response from {url}:\r\n{text}"); return(text); }
private static void GetRenderedWebPage(string url, TimeSpan waitAfterPageLoad, Action <string> callBack) { const string cEndLine = "All output received"; var sb = new StringBuilder(); var p = new PhantomJS(); p.OutputReceived += (sender, e) => { if (e.Data == cEndLine) { callBack(sb.ToString()); } else { sb.AppendLine(e.Data); } }; p.RunScript(@" var page = require('webpage').create(); page.viewportSize = { width: 1920, height: 1080 }; page.onLoadFinished = function(status) { if (status=='success') { setTimeout(function() { console.log(page.content); console.log('" + cEndLine + @"'); phantom.exit(); }," + waitAfterPageLoad.TotalMilliseconds + @"); } }; var url = '" + url + @"'; page.open(url);", new string[0]); }
static void Main(string[] args) { var phantomJS = new PhantomJS(); // write result to stdout Console.WriteLine("Getting content from baidu.com directly to C# code..."); var outFileHtml = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bilibili.html"); if (File.Exists(outFileHtml)) { File.Delete(outFileHtml); } using (var outFs = new FileStream(outFileHtml, FileMode.OpenOrCreate, FileAccess.Write)) { try { phantomJS.RunScript(@" var system = require('system'); var page = require('webpage').create(); page.open('https://www.bilibili.com/', function() { system.stdout.writeLine(page.content); page.render('cutPic.png'); phantom.exit(); }); " , null, null, outFs); } finally { phantomJS.Abort(); // ensure that phantomjs.exe is stopped } } Console.WriteLine("Result is saved into " + outFileHtml); }
static void Main(string[] args) { var phantomJs = new PhantomJS(); phantomJs.OutputReceived += (sender, e) => { Console.WriteLine($"PhantomJS output: {e.Data}"); }; phantomJs.RunScript("for (var i=0; i<10; i++) console.log('hello from js '+i); phantom.exit();", null); }
void GetAmazonImageUrls(String url) { var result = new ArrayList(); var phantomJS = new PhantomJS(); phantomJS.OutputReceived += (sender, e) => { if (e.Data != null) { if (e.Data.StartsWith("result__")) { AddToTextArea(e.Data.Replace("result__", "")); } Console.WriteLine("PhantomJS output: {0}", e.Data); } }; phantomJS.ErrorReceived += (sender, e) => { Console.WriteLine("PhantomJS error: {0}", e.Data); }; var scriptString = @" var webPage = require('webpage'); var system = require('system'); var args = system.args; var page = webPage.create(); page.onConsoleMessage = function(msg) { console.log(msg); } page.open('" + url + @"', function(status) { var url= page.evaluate(function() { return document.getElementsByClassName('itemNo0')[0].getElementsByTagName('img')[0].src }); console.log('result__' + url); phantom.exit(); }); "; resultLabel.Text = "取得中"; phantomJS.RunScript(scriptString, null); }
public static void RunPaymentScript(StartPaymentOperationResult result) { var phantomJS = new PhantomJS(); phantomJS.OutputReceived += (sender, e) => { if (e.Data != null) { throw new Exception($"Payment script failed: {e.Data}"); } }; using (var fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BarionClientLibrary.IntegrationTests.PaymentScript.js")) using (var streamReader = new StreamReader(fileStream)) { var paymentScript = streamReader.ReadToEnd(); phantomJS.RunScript(paymentScript, new[] { result.GatewayUrl, AppSettings.BarionPayer, AppSettings.BarionPayerPassword }); } }
/// <summary> /// Export of the reports to PDF /// </summary> /// <param name="url">Link to the report</param> /// <param name="orientation">Landscape or Portrait orientation</param> /// <returns>Link to the generated pdf file</returns> public static string ExportReportPdf(string url, string orientation) { var phantomJs = new PhantomJS(); string webUri = ConfigurationManager.AppSettings.Get("WebURI"); const string fileName = ""; const string filePath = "../Img/ReportsPdf/" + fileName; string serverPath = HttpContext.Current.Server.MapPath("~/Img/ReportsPdf/") + fileName; phantomJs.RunScript(@" var page = require('webpage').create(); page.viewportSize = { width: 1754, height: 1240}; page.settings.resourceTimeout = 20000; page.paperSize = { format: 'A4', orientation: '" + orientation + @"', margin: '1cm' }; page.open('" + webUri + "Account/Login" + @"', function() { page.open('" + url + @"', function() { setInterval(function(){ var title = page.evaluate(function() { $('nav').hide(); }); page.render('" + filePath + @"'); phantom.exit(); }, 5000); }); });", null); if (File.Exists(serverPath)) { return("Img/ReportsPdf/" + fileName); } return(""); }
private string AquireText(string strURL, string javascriptCode) { using (var ms = new MemoryStream()) { browser.RunScript(@" var system = require('system'); var page = require('webpage').create(); page.open('" + strURL + @"', function(status) { if(status === 'success') { var data = page.evaluate(function() { " + javascriptCode + @" }); system.stdout.writeLine('!~!' + data + '!~!'); } phantom.exit(); });", null, null, ms); ms.Position = 0; var sr = new StreamReader(ms); var result = sr.ReadToEnd(); result = result.Split(new [] { "!~!" }, StringSplitOptions.None)[1]; return(result); } }
/// <summary> /// 加载js动态内容 /// </summary> /// <param name="url"></param> /// <returns></returns> private static string GetJSContent(string url, string encode) { string text = ""; var phantomJS = new PhantomJS(); // write result to stdout //Console.WriteLine("Getting content from baidu.com directly to C# code..."); //var outFileHtml = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "baidu.html"); //if (File.Exists(outFileHtml)) // File.Delete(outFileHtml); using (var outFs = new MemoryStream()) { try { phantomJS.RunScript(string.Format(@" var system = require('system'); var page = require('webpage').create(); page.open('{0}', function() {{ system.stdout.writeLine(page.content); phantom.exit(); }}); ", url), null, null, outFs); byte[] b = outFs.ToArray(); text = System.Text.Encoding.GetEncoding(encode).GetString(b, 0, b.Length); } finally { phantomJS.Abort(); // ensure that phantomjs.exe is stopped } } //Console.WriteLine("Result is saved into " + outFileHtml); return(text); }
static void Main(string[] args) { var json = new JavaScriptSerializer(); var phantomJS = new PhantomJS(); phantomJS.OutputReceived += (sender, e) => { Console.WriteLine("PhantomJS output: {0}", e.Data); }; phantomJS.ErrorReceived += (sender, e) => { Console.WriteLine("PhantomJS error: {0}", e.Data); }; // provide custom input data to js code var inputData = json.Serialize(new[] { new User() { name = "Bob", age = 30, company = "AirBNB" }, new User() { name = "Alice", age = 27, company = "Yahoo" }, new User() { name = "Tom", age = 31, company = "Microsoft" } }); var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputData + "\n")); try { phantomJS.RunScript(@" var system = require('system'); console.log('read data...'); var inputData = system.stdin.readLine(); console.log('done'); var input = JSON.parse(inputData); for (var i=0; i<input.length; i++) { console.log('Name: '+input[i].name+' Age: '+input[i].age); } phantom.exit(); " , null, inputStream, null); } finally { phantomJS.Abort(); // ensure that phantomjs.exe is stopped } Console.WriteLine(); Console.WriteLine(); // write result to stdout Console.WriteLine("Getting content from google.com directly to C# code..."); var outFileHtml = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "google.html"); if (File.Exists(outFileHtml)) { File.Delete(outFileHtml); } using (var outFs = new FileStream(outFileHtml, FileMode.OpenOrCreate, FileAccess.Write)) { try { phantomJS.RunScript(@" var system = require('system'); var page = require('webpage').create(); page.open('https://google.com/', function() { system.stdout.writeLine(page.content); phantom.exit(); }); " , null, null, outFs); } finally { phantomJS.Abort(); // ensure that phantomjs.exe is stopped } } Console.WriteLine("Result is saved into " + outFileHtml); Console.WriteLine(); Console.WriteLine(); // execute rasterize.js var outFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "google.pdf"); if (File.Exists(outFile)) { File.Delete(outFile); } Console.WriteLine("Getting screenshot of google.com page..."); try { phantomJS.Run(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rasterize.js"), new[] { "http://nvd3.org/examples/discreteBar.html", outFile }); } finally { phantomJS.Abort(); } Console.WriteLine("Result is saved into " + outFile); }
public void Test(int i) { var phantomJS = new PhantomJS(); //phantomJS.OutputReceived += (sender, e) => //{ // Console.WriteLine("Got data from PhantomJS output: {0}", e.Data); //}; var resUrl = string.Format("http://{0}/Home/InvitationRoute?p={1}", "xiaobao1001.cn", HttpUtility.UrlEncode(string.Format("oldinvitenew?id={0}&iscutpage=true", 615))); //http://xiaobao1001.cn/Home/InvitationRoute?p=oldinvitenew?id=12418&iscutpage=true using (var outMs = new MemoryStream()) { try { var temp = getSS(); phantomJS.RunScript(temp, new string[] { resUrl, "640*1047" }, null, outMs); } catch (Exception ex) { } finally { phantomJS.Abort(); } var buffer = outMs.ToArray(); var imageInfo = Encoding.UTF8.GetString(buffer); if (!string.IsNullOrEmpty(imageInfo)) { buffer = Convert.FromBase64String(imageInfo); var ms = new MemoryStream(); ms.Write(buffer, 0, buffer.Length); var bitMap = new Bitmap(ms); var path = @"D:\work\He\CommonTest\PhantomJsConsole\bin\Debug\" + Guid.NewGuid(); bitMap.Save(path + ".jpg"); bitMap.Dispose(); ms.Close(); } } }
public static string ExportSvfMap(int tip) { var phantomJs = new PhantomJS(); string webUri = "http://*****:*****@" var page = require('webpage').create(); page.viewportSize = { width: " + pdfViewportWidth + @", height: " + pdfViewportHeight + @"}; page.settings.resourceTimeout = 60000; page.paperSize = { format: '" + paperFormat + @"', margin: '1cm', orientation: '" + pageOrientation + @"', width: '" + pdfViewportWidth + @"px', height: '" + pdfViewportHeight + @"px', }; page.open('" + theUrl + @"', function() { setInterval(function(){ page.render('" + filePath + @"'); phantom.exit(); }, 20000); }); ", null); if (File.Exists(serverPath)) { return(fileName); } return(""); }
private void ReadResponseHeaders(string url) { var scriptFile = Server.MapPath("~/Scripts/PhantomJS/ReadResponseHeaders.js"); var scriptSource = System.IO.File.ReadAllText(scriptFile); var phantomJs = new PhantomJS(); phantomJs.OutputReceived += (s, e) => { var response = e.Data; }; phantomJs.RunScript(@"var system = require('system'); var page = require('webpage').create(); page.onResourceReceived = function (response) { console.log('Response (#' + response.id + ', stage ""' + response.stage + '""): ' + JSON.stringify(response)); }; page.open(system.args[1]);", new string[] { url}); }