Advertisement
powerofsoul

Untitled

Oct 25th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApplication17 {
  10.     class Program {
  11.         const string url = "http://www.phonearena.com";
  12.         const string mainurl = "http://www.phonearena.com/phones/manufacturers";
  13.         const string fileName = "phones.txt";
  14.         static void Main (string[] args) {
  15.  
  16.             System.IO.File.WriteAllText(fileName,"");
  17.             foreach(String m in applyRegex(@"(?<=<a href="")\/phones\/manufacturers.*(?="" class)",getSource(mainurl))){
  18.                 int number = 1;
  19.                 Console.Write(number + " ");
  20.                 System.IO.File.AppendAllText(fileName,m.Split('/')[m.Split('/').ToList<string>().Count-1] + ":" + Environment.NewLine);
  21.                 string source = getSource(url+m);
  22.                 int maxPg = Convert.ToInt32(applyRegex(@"(?<=onclick='changePage\().?\d(?=, this.href)",source)[applyRegex(@"(?<=onclick='changePage\().?\d(?=, this.href)",source).Count-1]);
  23.                 string regexPhone = @"(?<=alt="").*?(?="" widht=""150"")";
  24.                 foreach(String s in applyRegex(regexPhone,source))
  25.                     System.IO.File.AppendAllText(fileName,s+" ");
  26.                 Console.Write(maxPg + ": ");
  27.                 for(int i = 2;i<=maxPg;i++) {
  28.                     Console.Write(i+" ");
  29.                     foreach(String s in applyRegex(regexPhone,getSource(url+m+"/page/"+i+"/")))
  30.                         System.IO.File.AppendAllText(fileName,s+" ");
  31.                 }
  32.                 System.IO.File.AppendAllText(fileName,Environment.NewLine);
  33.                 Console.Clear();
  34.                 number++;  
  35.             }
  36.            
  37.         }
  38.  
  39.         public static string getSource (string url) {
  40.             using(WebClient client = new WebClient()) {
  41.                 return client.DownloadString(url);
  42.             }
  43.         }
  44.         public static List<string> applyRegex (string regex,string sourcestring) {
  45.             Regex re = new Regex(regex);
  46.            
  47.             return re.Matches(sourcestring).Cast<Match>().Select(mm => mm.Value).Skip(1).Distinct().ToList<string>();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement