Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.23 KB | None | 0 0
  1. package test;
  2.  
  3. /**
  4.  *
  5.  * @author User
  6.  */
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStream;
  14. import java.io.OutputStreamWriter;
  15. import java.io.PrintWriter;
  16. import java.io.Writer;
  17. import static java.lang.Thread.sleep;
  18. import java.net.HttpURLConnection;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21. import java.net.URLConnection;
  22. import java.util.Scanner;
  23.  
  24. public class AllegroCoinsSearcher {
  25.    
  26.     public static void saveStringBuilderToCSV(StringBuilder string, String nameOfOutputFile) throws FileNotFoundException, IOException
  27.     {
  28.         OutputStream outputStream       = new FileOutputStream(nameOfOutputFile+".csv");
  29.         Writer       outputStreamWriter = new OutputStreamWriter(outputStream);
  30.         outputStreamWriter.write(string.toString());
  31.         outputStreamWriter.close();
  32.     }
  33.    
  34.     public static void makeSomeMoreLinks(File file) throws FileNotFoundException, IOException
  35.     {
  36.         StringBuilder collectedValue = new StringBuilder();
  37.         String filters = "?order=p&allegro-smart-standard=1&stan=nowe&super-sprzedawca=1&monety=1&offerTypeBuyNow=1&price_to=250";
  38.         String pagination = "&p=";
  39.         int howManyPages = 10;
  40.         try
  41.         {
  42.             Scanner read = new Scanner(file);
  43.             String readLine = "";
  44.             while ((readLine = read.nextLine()) != null)
  45.             {
  46.                 for (int i = 1; i < howManyPages; i++)
  47.                 {
  48.                     collectedValue.append(readLine+filters+pagination+i+"\n");
  49.                 }
  50.             }
  51.         }
  52.         catch (Exception e)
  53.         {
  54.             System.out.println("Error");
  55.         }
  56.         System.out.println("CollectedValue:");
  57.         System.out.println(collectedValue);
  58.         saveStringBuilderToCSV(collectedValue,"więcejLinków");
  59.     }
  60.  
  61.     public static void main(String[] args) throws MalformedURLException, IOException
  62.     {
  63. //        String inputLine;
  64. //        URL link = new URL("https://allegro.pl/kategoria/smartfony-i-telefony-komorkowe-165?price_to=500&offerTypeBuyNow=1&allegro-smart-standard=1&monety=1&super-sprzedawca=1&order=p");
  65. //        URLConnection con = link.openConnection();
  66. //        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
  67. //
  68. //        BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream(),"UTF-8"));
  69. //        int i = 1;
  70. //        while ((inputLine = in.readLine()) != null)
  71. //        {
  72. //            System.out.println("Linijka " + i + ": " + inputLine);
  73. //            i++;
  74. //        }  
  75. //        in.close();
  76. //      
  77.         File plik = new File("input.txt");
  78.         makeSomeMoreLinks(plik);
  79.         Scanner read = new Scanner(new File("input.txt"));
  80.        
  81.        
  82.         StringBuilder savedValues = new StringBuilder();
  83.         savedValues.append("Link;Ilość monet;Cena\n");
  84.         int index1, index2, index3; // zmienne liczbowe do wyszukiwania linków
  85.         String pharse1, pharse2, pharse3; //  zmienne tekstowe od wyszukiwania linków
  86.         String pharse4, pharse5, pharse6; // zmienne tekstowe do wyszukiwania ilości monet
  87.         int index4, index5; // zmienne liczbowe do wyszukiwania ilości monet
  88.         String inputLine; // zmienna wykorzystywana do przechowywania wczytanej lini z kodu źrodłowego strony
  89.         String readLine = "";
  90.        
  91.         try
  92.         {
  93.             while ((readLine = read.nextLine()) != null)
  94.             {
  95.                 System.out.println("Wczytana linia tekstu to: " + readLine); // readLine to link do strony
  96.                 URL link = new URL(readLine);
  97.                 URLConnection con = link.openConnection();
  98.                 con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
  99.  
  100.                 BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream(),"UTF-8"));
  101.                 int i = 1;
  102.                
  103.                 while ((inputLine = in.readLine()) != null)
  104.                 {  
  105.                     i++;
  106.                     String text = inputLine;
  107.                     int helpfulVariable = 0;
  108.  
  109.                     while(helpfulVariable != -1)
  110.                     {
  111.                         pharse1 = "https://allegro.pl/";
  112.                         pharse2 = ".html";
  113.                         index1 = text.indexOf(pharse1, helpfulVariable);
  114.  
  115.                         if(index1 != -1)
  116.                         {
  117.                             System.out.println("Znaleziono HTTPS. Nr indeksu to: " + index1);
  118.                             index2 = text.indexOf(pharse2, index1);
  119.                             System.out.println("Szukam .html");
  120.                             if(index2 != -1)
  121.                             {
  122.                                 System.out.println("Znaleziono .html na miejscu: " + index2);
  123.                                 pharse3 = text.substring(index1, index2+5);
  124.                                 System.out.println(pharse3);
  125.                                 helpfulVariable = index2;
  126.                                 if(pharse3.length()<150)
  127.                                 {
  128.                                    
  129.  
  130.                                     // Tutaj rozpoczniemy szukanie MONET
  131.                                     pharse4 = "otrzymasz<!-- --> <!-- -->";
  132.                                     pharse5 = "</span>";
  133.                                     System.out.println("Szukam frazy: otrzymasz<!-- --> <!-- -->");
  134.                                     index4 = text.indexOf(pharse4, helpfulVariable);
  135.                                     if(index4 != -1)
  136.                                     {
  137.                                         System.out.println("Znaleziono: otrzymasz<!-- --> <!-- -->");
  138.                                         System.out.println("Miejsce znalezionego indexu dla otrzymasz to: " + index4);
  139.                                         index5 = text.indexOf(pharse5, index4);
  140.                                         System.out.println("Miejsce znalezionego indexu dla SPAN to: " + index5);
  141.                                         pharse6 = text.substring(index4+pharse4.length(),index5);
  142.                                         System.out.println("Znaleziona fraza od otrzymasz -> SPAN to: " + pharse6);
  143.                                         helpfulVariable = index5;
  144.                                         if(pharse5.length()<45)
  145.                                         {
  146.                                             savedValues.append(pharse3);
  147.                                             savedValues.append(";");
  148.                                            
  149.                                             savedValues.append(pharse6);
  150.                                             savedValues.append(";\n");
  151.                                         }
  152.                                     }
  153.                                 }
  154.                             }
  155.                             else
  156.                             {
  157.                                 helpfulVariable = -1;
  158.                             }
  159.                         }
  160.                         else
  161.                         {
  162.                             System.out.println("W tej linii tekstu nie znaleziono HTTPS.");
  163.                             helpfulVariable = -1;
  164.                         }
  165.                     }
  166.                 }
  167.                 sleep(2000);
  168.             }
  169.         }
  170.         catch (Exception e)
  171.         {
  172.             System.out.println("Koniec wczytywania z pliku.");
  173.            
  174.         };
  175.        
  176.         System.out.println("-----Wynik:-----");
  177.         System.out.println(savedValues);
  178.        
  179. //        // Zapis
  180. //        OutputStream outputStream       = new FileOutputStream("outputTERAZ.csv");
  181. //        Writer       outputStreamWriter = new OutputStreamWriter(outputStream);
  182. //        outputStreamWriter.write(savedValues.toString());
  183. //        outputStreamWriter.close();
  184.        
  185.         saveStringBuilderToCSV(savedValues,"gotowyArkusz");
  186.     }
  187.    
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement