Skip to content

模拟浏览器访问

L edited this page Nov 2, 2019 · 2 revisions

PuppeteerSharp
中文讲解

关键代码

//简单示例
public static string GetPageContent(string ip,int port, string url, int loadTime)
{
	var chromeInstance = ResetChromeInstance(ip, port);
	Page page = chromeInstance.NewPageAsync().Result; //每个用户独立页面,互不干扰。
	string pageResult = null;
	try
	{
		page.SetRequestInterceptionAsync(true).Wait();
		page.Request += (sender, e) =>
		{
			if (e.Request.ResourceType == ResourceType.Image)
				e.Request.AbortAsync();
			else
				e.Request.ContinueAsync();
		};
		var response = page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded).Result; //比默认的Load快;
		pageResult = page.GetContentAsync().Result;
		page.WaitForTimeoutAsync(loadTime).Wait();
		Console.WriteLine($"{ip} {port} {url} success:wait {loadTime}");
		page.Dispose();
	}
	catch (Exception ex)
	{
		Console.WriteLine($"{ip} {port} {url} failed:{ex.InnerException.Message}");
	}
	return pageResult;
}

public static Browser ResetChromeInstance(string ip, int port)
{
	#region 确保browser运行环境 
	//string currentDirectory = @"/www/wwwroot/chrome-linux/chrome";//CentOS下直接指定
	string UserName = Environment.UserName;
	string currentDirectory = Path.Combine(@"C:\Users\", UserName, @"AppData\Local\Google\Chrome\Application\", "Chrome.exe"); //先找机器上是否有Chrome,优先利用它。
	if (!File.Exists(currentDirectory)) //若没有,考虑使用Chromium运行环境。
	{
		currentDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
		var downloadPath = Path.Combine(currentDirectory, "LocalChromium");
		Console.WriteLine($"Attemping to set up puppeteer to use Chromium found under directory {downloadPath} ");
		if (!Directory.Exists(downloadPath)) //无本地Chromium运行环境。
		{
			Console.WriteLine("Custom directory not found. Creating directory");
			Directory.CreateDirectory(downloadPath);
			Console.WriteLine("Downloading Chromium");
			var browserFetcherOptions = new BrowserFetcherOptions { Path = downloadPath };
			var browserFetcher = new BrowserFetcher(browserFetcherOptions);
			browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).Wait(); //等待下载完毕
			var executablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision);
			if (string.IsNullOrEmpty(executablePath))
			{
				throw new Exception("Custom Chromium location is empty. Unable to start Chromium.");
			}
			Console.WriteLine($"Attemping to start Chromium using executable path: {executablePath}");
			currentDirectory = Path.Combine(executablePath);
		}
		else
		{
			currentDirectory = Path.Combine(downloadPath + @"\Win64-686378\chrome-win", "chrome.exe"); //已存Chromium运行环境。
		}
	}
	#endregion

	#region Browser运行选项
	var options = new LaunchOptions
	{
		Headless = true,//则浏览器交互可见。
		ExecutablePath = currentDirectory,//本地路径
										  //DefaultViewport = new ViewPortOptions
										  //{
										  //    Width = 500,
										  //    Height = 500,
										  //    IsMobile = false,
										  //    DeviceScaleFactor = 2
										  //},
		Args = new string[]
		{
			//"--disable-infobars",   //隐藏 自动化标题
			//"--ignore-certificate-errors",
			$"--proxy-server={ip}:{port}",
		},
	};
	#endregion

	var chromeInstance = Puppeteer.LaunchAsync(options).Result;//新生成浏览器实例
	return chromeInstance;
}
Clone this wiki locally