H4T3D

OLX web Scaper

Mar 21st, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 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.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace OLX_Scraper
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.        
  24.         string  getwebsitecontent(string url)
  25.         {
  26.             string html = String.Empty;
  27.             WebRequest request = WebRequest.Create(url);
  28.             WebResponse response = request.GetResponse();
  29.             Stream data = response.GetResponseStream();
  30.            
  31.             using (StreamReader sr = new StreamReader(data))
  32.             {
  33.                 html =  sr.ReadToEnd();
  34.             }
  35.            
  36.             return html+"hjhgjhgjh";
  37.            
  38.         }
  39.  
  40.         private void button1_Click(object sender, EventArgs e)
  41.         {
  42.             bg.CancelAsync();
  43.         }
  44.  
  45.         private void button2_Click(object sender, EventArgs e)
  46.         {
  47.            
  48.  
  49.         }
  50.  
  51.         BackgroundWorker bg = new BackgroundWorker();
  52.  
  53.         private void button3_Click(object sender, EventArgs e)
  54.         {
  55.            
  56.            
  57.                 for (int pages = 1; pages <= numericUpDown1.Value; pages++)
  58.                 {
  59.                     string url = "https://www.olx.com.pk/mobile-phones/?page=" + pages;
  60.  
  61.                    
  62.                     var links = Regex.Matches(getwebsitecontent(url).ToString(), "<a href=\"([^<]*)\" class=\"marginright5 link linkWithHash detailsLink\">");
  63.                     foreach (Match match in links)
  64.                     {
  65.          
  66.                         richTextBox1.Text += match.Groups[1].Value + "\n";
  67.    
  68.                     }
  69.  
  70.                     var location = Regex.Matches(getwebsitecontent(url).ToString(), "<span>([^<]*)</span>                                                       </small>");
  71.                     foreach (Match match in location)
  72.                     {
  73.                         richTextBox2.Text += match.Groups[1].Value + "\n";
  74.  
  75.                     }
  76.  
  77.  
  78.                     var price = Regex.Matches(getwebsitecontent(url).ToString(), "<strong class=\"c000\">([^<]*)</strong>");
  79.                     foreach (Match match in price)
  80.                     {
  81.  
  82.                         richTextBox3.Text += match.Groups[1].Value.Trim().Trim().Trim().Trim().Trim().Trim() + '\n';
  83.  
  84.                     }
  85.  
  86.                     var time = Regex.Matches(getwebsitecontent(url).ToString(), "<p class=\"color-9 lheight14 margintop3 small\">([^<]*)</p>");
  87.  
  88.                     foreach (Match match in time)
  89.                     {
  90.  
  91.                         richTextBox4.Text += match.Groups[1].Value + "\n";
  92.  
  93.                     }
  94.  
  95.                     char delimiterChars = '\n';
  96.                     string[] linker = richTextBox1.Text.Split(delimiterChars);
  97.                     string[] locationer = richTextBox2.Text.Split(delimiterChars);
  98.                     string[] pricer = richTextBox3.Text.Split(delimiterChars);
  99.                     string[] eimerx = richTextBox4.Text.Split(delimiterChars);
  100.                     progressBar1.Maximum = linker.Length - 1;
  101.                     for (int i = 0; i < linker.Length - 1; i++)
  102.                     {
  103.                         if (i == 0) { i++; }
  104.                         progressBar1.Value = i;
  105.                         this.dataGridView1.Rows.Add(i, linker[i], locationer[i], pricer[i], eimerx[i]);
  106.                     }
  107.                 }
  108.            
  109.            
  110.         }
  111.         //5d329
  112.  
  113.         private void button6_Click(object sender, EventArgs e)
  114.         {
  115.             char delimiterChars = '\n';
  116.             string[] urlz = richTextBox5.Text.Split(delimiterChars);
  117.  
  118.             for (int i = 0; i < urlz.Length - 1; i++)
  119.             {
  120.                 richTextBox6.Text += getwebsitecontent(urlz[i].ToString().Trim('\n'));
  121.             }
  122.         }
  123.     }
  124. }
Add Comment
Please, Sign In to add comment