Created
January 2, 2019 20:18
-
-
Save selcukusta/84942ca82acb966d0b8a15635880fd15 to your computer and use it in GitHub Desktop.
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 System; | |
| using System.Net.Http; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| /* packages.config */ | |
| /* | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <packages> | |
| <package id="HtmlAgilityPack" version="1.8.11" targetFramework="net472" /> | |
| <package id="System.Buffers" version="4.5.0" targetFramework="net472" /> | |
| <package id="System.Memory" version="4.5.1" targetFramework="net472" /> | |
| <package id="System.Net.Http.WinHttpHandler" version="4.5.1" targetFramework="net472" /> | |
| <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" /> | |
| <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" /> | |
| </packages> | |
| */ | |
| namespace Amazon.ConsoleApp | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| using (HttpClient client = new HttpClient(new AmazonHttp2Crawler())) | |
| { | |
| client.BaseAddress = new Uri("https://www.amazon.com"); | |
| //client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); | |
| //client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, sdch, br"); | |
| //client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.8"); | |
| //client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); | |
| var getAsyncRequest = Task.Run(() => client.GetAsync("dp/B079K9BZKF")); | |
| getAsyncRequest.Wait(); | |
| var getHttpResponse = getAsyncRequest.Result; | |
| var getResultContent = Task.Run(() => getHttpResponse.Content.ReadAsStringAsync()); | |
| getResultContent.Wait(); | |
| var htmlContent = getResultContent.Result; | |
| HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); | |
| doc.LoadHtml(htmlContent); | |
| var productName = doc.DocumentNode.SelectSingleNode("//*[@id='productTitle']").InnerText.Trim(); | |
| var price = doc.DocumentNode.SelectSingleNode("//*[@id='priceblock_ourprice']").InnerText; | |
| Console.WriteLine($"{productName} - {price}"); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } | |
| public class AmazonHttp2Crawler : WinHttpHandler | |
| { | |
| protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
| { | |
| //request.Version = new Version("2.0"); | |
| /* Amazon, response'u gzip ile gonderdigi icin asagidaki satir ile gelen cevabi decompress etmek zorundayiz! */ | |
| AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate; | |
| return base.SendAsync(request, cancellationToken); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment