Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using HtmlAgilityPack;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace GeneralMK.Forms
- {
- public partial class Olx : Form
- {
- public Olx()
- {
- InitializeComponent();
- }
- public Thread t;
- private void bunifuThinButton21_Click(object sender, EventArgs e)
- {
- this.t = new Thread(new ThreadStart(this.StartScrape));
- this.t.IsBackground = true;
- this.t.Start();
- }
- private void bunifuThinButton22_Click(object sender, EventArgs e)
- {
- this.t.Abort();
- MessageBox.Show("Thread Has Been Aported!", "Stopped");
- }
- private void bunifuThinButton23_Click(object sender, EventArgs e)
- {
- this.metroGrid1.Rows.Clear();
- }
- private void bunifuThinButton24_Click(object sender, EventArgs e)
- {
- this.saveData();
- MessageBox.Show("Data Had Saved Succesfully", "Saved");
- }
- public void saveData()
- {
- using (SaveFileDialog saveFileDialog = new SaveFileDialog
- {
- Title = "Save Data",
- FileName = string.Format("{0} data.csv", this.metroGrid1.Rows.Count),
- Filter = "csv|*.csv;"
- })
- {
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- List<string> list = new List<string> { "url,title,price,date,city,phone" };
- foreach (DataGridViewRow row in (IEnumerable)this.metroGrid1.Rows)
- {
- try
- {
- string str2 = row.Cells[0].Value.ToString().Trim();
- string str3 = row.Cells[1].Value.ToString().Trim();
- string str4 = row.Cells[2].Value.ToString().Trim();
- string str5 = row.Cells[3].Value.ToString().Trim();
- string str6 = row.Cells[4].Value.ToString().Trim();
- string str7 = row.Cells[5].Value.ToString().Trim();
- //list.Add(string.Format(""{str2}","{str3}","{str4}","{str5}","{str6}","{str7}""), new object[]
- //{
- // str2,
- // str3,
- // str4,
- // str5,
- // str6,
- // str7
- //}));
- }
- catch
- {
- }
- }
- string text5 = string.Empty;
- foreach (string str in list)
- {
- text5 = text5 + str + Environment.NewLine;
- }
- File.Create(saveFileDialog.FileName).Close();
- File.AppendAllText(saveFileDialog.FileName, text5, Encoding.UTF8);
- }
- }
- }
- public void StartScrape()
- {
- int num = 0;
- try
- {
- for (int i = 1; i <= int.Parse(this.bunifuMetroTextbox2.Text); i++)
- {
- string url = this.bunifuMetroTextbox1.Text + "?page=" + i.ToString();
- foreach (HtmlNode node in (IEnumerable<HtmlNode>)new HtmlWeb().Load(url).DocumentNode.SelectNodes("//div[@class=\"ads__item\"]"))
- {
- try
- {
- char[] separator = new char[] { '=' };
- string str2 = node.Attributes["onclick"].Value.Split(separator)[1].Replace("'", string.Empty);
- string[] textArray1 = new string[] { "ID" };
- char[] chArray2 = new char[] { '.' };
- string str3 = str2.Split(textArray1, StringSplitOptions.None)[1].Split(chArray2)[0];
- string innerText = node.SelectSingleNode(".//*[@class='ads__item__title']").InnerText;
- string str5 = node.SelectSingleNode(".//*[@class='ads__item__price price ']").InnerText;
- string str6 = node.SelectSingleNode(".//*[@class='ads__item__date']").InnerText;
- string str7 = node.SelectSingleNode(".//*[@class='ads__item__location']").InnerText;
- string address = "https://olx.com.eg/ajax/misc/contact/phone/" + str3;
- char[] chArray3 = new char[] { ':' };
- char[] chArray4 = new char[] { '"' };
- string str9 = new WebClient().DownloadString(address).Split(chArray3)[1].Split(chArray4)[1].Replace(" ", string.Empty);
- this.metroGrid1.Invoke(new Action(delegate
- {
- this.metroGrid1.Rows.Add();
- }));
- this.metroGrid1.Rows[num].Cells[0].Value = str2;
- this.metroGrid1.Rows[num].Cells[1].Value = innerText;
- this.metroGrid1.Rows[num].Cells[2].Value = str5;
- this.metroGrid1.Rows[num].Cells[3].Value = str6;
- this.metroGrid1.Rows[num].Cells[4].Value = str7;
- this.metroGrid1.Rows[num].Cells[5].Value = str9;
- num++;
- }
- catch
- {
- }
- }
- }
- MessageBox.Show("Completed Succesfully!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- catch (Exception exception2)
- {
- MessageBox.Show(exception2.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement