Created
September 24, 2018 04:32
-
-
Save ksasao/b3b2105ac9f7012af12bbb26c030888e to your computer and use it in GitHub Desktop.
PuppeteerSharp を利用して、特定のURLをヘッドレスブラウザでダウンロードする C# コード。ビルド時には nuget で PuppeteerSharp を追加する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using PuppeteerSharp; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace ChromeDownload | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| if (args.Length != 2) | |
| { | |
| Console.WriteLine("usage: ChromeDownload \"URL\" download_path"); | |
| return; | |
| } | |
| string html = DownloadUrl(args[0]).Result; | |
| File.WriteAllText(args[1], html); | |
| } | |
| static async Task<string> DownloadUrl(string url) | |
| { | |
| await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); | |
| var browser = await Puppeteer.LaunchAsync(new LaunchOptions | |
| { | |
| Headless = true | |
| }); | |
| var page = await browser.NewPageAsync(); | |
| await page.SetViewportAsync(new ViewPortOptions { Width = 1080, Height = 1920 }); // 縦長の画面を設定 | |
| await page.GoToAsync(url); | |
| string html = await page.GetContentAsync(); | |
| page.Dispose(); | |
| browser.Dispose(); | |
| return html; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment