Пример #1
0
        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()));
            }
        }
Пример #2
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);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        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);
        }
Пример #5
0
 protected void CleanUpBrowser()
 {
     browser.Abort();
 }
Пример #6
0
        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();
                }
            }
        }