.net爬虫了解一下
2018-07-04 18:36
387 查看
using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; using OpenQA.Selenium; using System.IO; using System.Collections.Generic; using System.Text; using System.Threading; using System.Net; using HtmlAgilityPack; namespace ConsoleApplication1 { class requests { public static string UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"; public static string AcceptLang = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"; public static bool AddProxy = false; public static bool ProxyPort = false; public static string ProxyHost = null; private static string Status; private static string ResponseUri; private static HtmlDocument Document=new HtmlDocument(); static PhantomJSDriver driver { get; set; } static void Main(string[] args) { //PhantomJSDriverService driverService = PhantomJSDriverService.CreateDefaultService(); ////driverService.IgnoreSslErrors = true; //"any" also works //driverService.HideCommandPromptWindow = true; //driverService.ProxyType = "none"; //driverService.SslProtocol = "any"; //driverService.MaxDiskCacheSize = 1000; //driverService.DiskCache = true; //driverService.WebSecurity = false; //PhantomJSOptions phs = new PhantomJSOptions(); //phs.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"); var req = get(new Uri("http://www.baidu.com")); var resp = response(req); var root = GetResult(resp); var nodelist=parse(root,"//div"); Console.WriteLine(root.DocumentNode.InnerHtml); ChromeOptions options = new ChromeOptions(); options.AddArgument("--headless"); options.AddArgument("--nogpu"); List<String> tagNmaeList = new List<string>(); ; //using (driver = new PhantomJSDriver(driverService, phs)) //{ using (var driver = new ChromeDriver(options)) { driver.Manage().Window.Maximize(); driver.Navigate().GoToUrl(@"https://qiita.com/ttake/items/2e470462f8d90e76e996"); Thread.Sleep(5000); string source = driver.PageSource; Console.WriteLine(driver.Url); var html = driver.FindElement(By.TagName("html")); try { var div = driver.FindElement(By.Id("global-nav")); } catch (NoSuchElementException) { Console.WriteLine("找不到该元素"); ; } IJavaScriptExecutor js = (IJavaScriptExecutor)driver; var divnode = driver.FindElement(By.ClassName("highlight")); var bkgimg = js.ExecuteScript("arguments[0].style.getPropertyValue('background-image')", divnode); if (bkgimg == null) { var outerhtml = divnode.GetAttribute("outerHTML"); //outerhtml var outerText = divnode.GetAttribute("outerText"); var innerText = divnode.GetAttribute("innerText"); var children = divnode.GetAttribute("children"); var textContent = divnode.GetAttribute("textContent"); var childElementCount = divnode.GetAttribute("childElementCount"); } var image = driver.FindElement(By.ClassName("it-Header_authorImage")); var width = image.Size.Width; var height = image.Size.Height; var elem = js.ExecuteScript("return document.defaultView.getComputedStyle(arguments[0])", image); Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot(); var dirpath = @"D:\temp"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } screenshot.SaveAsFile(@"D:\temp\seleniumScreenshot.png", ScreenshotImageFormat.Png); } } private static HttpWebRequest get(Uri uri) { HttpWebRequest httpWebRequest = WebRequest.Create(uri) as HttpWebRequest; if (httpWebRequest != null) { httpWebRequest.AllowAutoRedirect = true; httpWebRequest.AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate); httpWebRequest.UserAgent = UserAgent; if (!string.IsNullOrEmpty(AcceptLang)) { httpWebRequest.Headers.Add("Accept-Language", AcceptLang); } if (AddProxy) { WebProxy proxy = new WebProxy(ProxyHost, ProxyPort); httpWebRequest.Proxy = proxy; } } return httpWebRequest; } private static HttpWebResponse response(WebRequest request) { return (HttpWebResponse)request.GetResponse(); } private static HtmlDocument GetResult(HttpWebResponse response) { if (response != null) { Status = response.StatusCode.ToString().ToUpper(); ResponseUri = response.ResponseUri.AbsoluteUri; using (Stream responseStream = response.GetResponseStream()) { byte[] array = new byte[1024]; using (MemoryStream memoryStream = new MemoryStream()) { int num; do { num = responseStream.Read(array, 0, array.Length); if (num > 0) { memoryStream.Write(array, 0, num); } } while (num > 0); memoryStream.Seek(0L, SeekOrigin.Begin); Document.Load(memoryStream,Encoding.UTF8); } } } return Document; } private static HtmlNodeCollection parse(HtmlDocument doc,string xpath) { var nodelist=doc.DocumentNode.SelectNodes(xpath); return nodelist; } } }
相关文章推荐
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- 了解一下爬虫技术方方面面
- .Net常用事务(了解一下)
- Python学习,多进程了解一下!学爬虫不会用多进程能行吗?
- 了解一下敏捷开发
- 转载:深入了解.NET中堆和栈的比较--二
- 大家了解纸箱包装设备吗?下面让我们一起学习一下吧!
- 什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式?
- ZZ:使用.NET语言开发Silverlight应用程序入门(一):了解项目结构
- 智慧解析第17集:了解一下逻辑
- 一篇文章了解爬虫技术现状
- Linux强大的网络功能是如何实现的,让我们一起进入Linux内核的网络系统了解一下吧。
- 你真的了解.NET中的String吗?
- 了解一下NTLM
- Python爬虫教程——入门一之爬虫基础了解
- 排序方法了解一下(冒泡排序、选择排序、堆排序、插入排序、希尔排序、归并排序、快速排序、基数排序)
- 了解一下Resin
- 网络爬虫(一):爬虫的含义和URL基本构成——(了解)
- LED灯带工程安装成本计算三步骤   LED灯带主要是用于装饰,由于对灯带不是很了解,因此有很多客户会在计算工程报价的时候漏掉一些环节,给自己增加了一些不必要的成本。下面就分享一下如何计算灯带的工程安
- 不了解一下人工智能的发展历史,又怎么能看明白 AlphaGo 的对战呢