Advertisement
AhmedSokr

Untitled

Mar 9th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.31 KB | None | 0 0
  1. using HtmlAgilityPack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15.  
  16. namespace GeneralMK.Forms
  17. {
  18.     public partial class Olx : Form
  19.     {
  20.         public Olx()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.         public Thread t;
  25.         private void bunifuThinButton21_Click(object sender, EventArgs e)
  26.         {
  27.             this.t = new Thread(new ThreadStart(this.StartScrape));
  28.             this.t.IsBackground = true;
  29.             this.t.Start();
  30.         }
  31.  
  32.         private void bunifuThinButton22_Click(object sender, EventArgs e)
  33.         {
  34.             this.t.Abort();
  35.             MessageBox.Show("Thread Has Been Aported!", "Stopped");
  36.         }
  37.  
  38.         private void bunifuThinButton23_Click(object sender, EventArgs e)
  39.         {
  40.             this.metroGrid1.Rows.Clear();
  41.         }
  42.  
  43.         private void bunifuThinButton24_Click(object sender, EventArgs e)
  44.         {
  45.             this.saveData();
  46.             MessageBox.Show("Data Had Saved Succesfully", "Saved");
  47.         }
  48.         public void saveData()
  49.         {
  50.             using (SaveFileDialog saveFileDialog = new SaveFileDialog
  51.             {
  52.                 Title = "Save Data",
  53.                 FileName = string.Format("{0} data.csv", this.metroGrid1.Rows.Count),
  54.                 Filter = "csv|*.csv;"
  55.             })
  56.             {
  57.                 if (saveFileDialog.ShowDialog() == DialogResult.OK)
  58.                 {
  59.                     List<string> list = new List<string> { "url,title,price,date,city,phone" };
  60.                     foreach (DataGridViewRow row in (IEnumerable)this.metroGrid1.Rows)
  61.                     {
  62.                         try
  63.                         {
  64.                             string str2 = row.Cells[0].Value.ToString().Trim();
  65.                             string str3 = row.Cells[1].Value.ToString().Trim();
  66.                             string str4 = row.Cells[2].Value.ToString().Trim();
  67.                             string str5 = row.Cells[3].Value.ToString().Trim();
  68.                             string str6 = row.Cells[4].Value.ToString().Trim();
  69.                             string str7 = row.Cells[5].Value.ToString().Trim();
  70.                             //list.Add(string.Format(""{str2}","{str3}","{str4}","{str5}","{str6}","{str7}""), new object[]
  71.                             //{
  72.                             //    str2,
  73.                             //    str3,
  74.                             //    str4,
  75.                             //    str5,
  76.                             //    str6,
  77.                             //    str7
  78.                             //}));
  79.                         }
  80.                         catch
  81.                         {
  82.                         }
  83.                     }
  84.                     string text5 = string.Empty;
  85.                     foreach (string str in list)
  86.                     {
  87.                         text5 = text5 + str + Environment.NewLine;
  88.                     }
  89.                     File.Create(saveFileDialog.FileName).Close();
  90.                     File.AppendAllText(saveFileDialog.FileName, text5, Encoding.UTF8);
  91.                 }
  92.             }
  93.         }
  94.         public void StartScrape()
  95.         {
  96.             int num = 0;
  97.             try
  98.             {
  99.                 for (int i = 1; i <= int.Parse(this.bunifuMetroTextbox2.Text); i++)
  100.                 {
  101.                     string url = this.bunifuMetroTextbox1.Text + "?page=" + i.ToString();
  102.                     foreach (HtmlNode node in (IEnumerable<HtmlNode>)new HtmlWeb().Load(url).DocumentNode.SelectNodes("//div[@class=\"ads__item\"]"))
  103.                     {
  104.                         try
  105.                         {
  106.                             char[] separator = new char[] { '=' };
  107.                             string str2 = node.Attributes["onclick"].Value.Split(separator)[1].Replace("'", string.Empty);
  108.                             string[] textArray1 = new string[] { "ID" };
  109.                             char[] chArray2 = new char[] { '.' };
  110.                             string str3 = str2.Split(textArray1, StringSplitOptions.None)[1].Split(chArray2)[0];
  111.                             string innerText = node.SelectSingleNode(".//*[@class='ads__item__title']").InnerText;
  112.                             string str5 = node.SelectSingleNode(".//*[@class='ads__item__price price ']").InnerText;
  113.                             string str6 = node.SelectSingleNode(".//*[@class='ads__item__date']").InnerText;
  114.                             string str7 = node.SelectSingleNode(".//*[@class='ads__item__location']").InnerText;
  115.                             string address = "https://olx.com.eg/ajax/misc/contact/phone/" + str3;
  116.                             char[] chArray3 = new char[] { ':' };
  117.                             char[] chArray4 = new char[] { '"' };
  118.                             string str9 = new WebClient().DownloadString(address).Split(chArray3)[1].Split(chArray4)[1].Replace(" ", string.Empty);
  119.  
  120.  
  121.                             this.metroGrid1.Invoke(new Action(delegate
  122.                             {
  123.                                 this.metroGrid1.Rows.Add();
  124.                             }));
  125.                             this.metroGrid1.Rows[num].Cells[0].Value = str2;
  126.                             this.metroGrid1.Rows[num].Cells[1].Value = innerText;
  127.                             this.metroGrid1.Rows[num].Cells[2].Value = str5;
  128.                             this.metroGrid1.Rows[num].Cells[3].Value = str6;
  129.                             this.metroGrid1.Rows[num].Cells[4].Value = str7;
  130.                             this.metroGrid1.Rows[num].Cells[5].Value = str9;
  131.                             num++;
  132.                         }
  133.                         catch
  134.                         {
  135.                         }
  136.                     }
  137.                 }
  138.                 MessageBox.Show("Completed Succesfully!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  139.             }
  140.             catch (Exception exception2)
  141.             {
  142.                 MessageBox.Show(exception2.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  143.             }
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement