Advertisement
kilya

HTTP Get request in C# v 1.0 - WebScrapping

Jan 11th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net.Http;
  11. using HtmlAgilityPack;
  12.  
  13. namespace WebScrapping
  14. {
  15.     public partial class frmHttp : Form
  16.     {
  17.         public class NamesAndScores
  18.         {
  19.             public string Title { get; set; }
  20.             public string Paragraph { get; set; }
  21.         }
  22.  
  23.         DataTable table;
  24.         HtmlWeb web = new HtmlWeb();
  25.  
  26.         public frmHttp()
  27.         {
  28.             InitializeComponent();
  29.             InitialTable();
  30.         }
  31.  
  32.         private async Task<List<NamesAndScores>> GetInfo(int pageNum)
  33.         {
  34.             string url = "https://www.hespress.com/politique/";
  35.             if (pageNum != 0)
  36.                 url = "https://www.hespress.com/politique/index." + pageNum.ToString() + ".html";
  37.             var doc = await Task.Factory.StartNew(() => web.Load(url));
  38.             var titleNodes = doc.DocumentNode.SelectNodes("//*[@id='box_center_holder']/div[6]/div/h2/a");
  39.             var paragraphNodes = doc.DocumentNode.SelectNodes("//*[@id='box_center_holder']/div[4]/div/p");
  40.             if (titleNodes == null || paragraphNodes == null)
  41.                 return new List<NamesAndScores>();
  42.  
  43.             var titles = titleNodes.Select(node => node.InnerText.Replace("&quot;", "\""));
  44.             var paragraphes = paragraphNodes.Select(node => node.InnerText.Replace("&quot;", "\"").Replace("&nbsp;", Environment.NewLine));
  45.             return titles.Zip(paragraphes, (title, paragraph) => new NamesAndScores() { Title = title, Paragraph = paragraph }).ToList();
  46.         }
  47.  
  48.         private async void btnGet_Click(object sender, EventArgs e)
  49.         {
  50.             try
  51.             {
  52.                 this.Cursor = Cursors.WaitCursor;
  53.                 btnGet.Enabled = false;
  54.                 int pageNum = 0;
  55.                 var getinfos = await GetInfo(pageNum);
  56.                 while (getinfos.Count > 0)
  57.                 {
  58.                     foreach (var getinfo in getinfos)
  59.                         table.Rows.Add(getinfo.Title, getinfo.Paragraph);
  60.                     pageNum++;
  61.                     getinfos = await GetInfo(pageNum);
  62.                 }
  63.                 this.Cursor = Cursors.Hand;
  64.                 btnGet.Enabled = true;
  65.             }
  66.             catch (Exception ex)
  67.             {
  68.                 MessageBox.Show(ex.Message);
  69.             }    
  70.         }
  71.  
  72.         private void InitialTable()
  73.         {
  74.             table = new DataTable("MyTable");
  75.             table.Columns.Add("Title of article", typeof(string));
  76.             table.Columns.Add("Excerpt from the article", typeof(string));
  77.             dataGridView1.DataSource = table;
  78.         }
  79.  
  80.         private void btnExample_Click(object sender, EventArgs e)
  81.         {
  82.             ExampleWebScrapping webScraper = new ExampleWebScrapping();
  83.             webScraper.ShowDialog();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement