08.11.2012 10:52
 0просмотров 19 0

Исходный код.

Чтобы было с чем сравнивать, приведу код, который должен был получиться, если все шаги выполнены верно:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

/**
*
* @author EmptyHead
*/
public class HWM_PersScanner {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO code application logic here
        File file  = new File("output.csv");
        PrintStream printStream = new PrintStream(new FileOutputStream(file));
        System.setOut(printStream);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                HWM_PersScanner scanner = new HWM_PersScanner();
                scanner.ScanDiapason(1, 10000);
            }
        });
        
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                HWM_PersScanner scanner = new HWM_PersScanner();
                scanner.ScanDiapason(10000, 20000);
            }
        });
        
        t1.start();
        t2.start();
    }
    
    private void ScanDiapason(int start_value, int stop_value){
        for (int i = start_value; i <= stop_value; i++ ){
            GetPage("http://www.heroeswm.ru/pl_info.php?id=" + i);
        }
    }

    
    private void GetPage(String Address){
        try{
            URL url = new URL((new URI(Address)).toASCIIString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);

            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1;WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1");
            conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            conn.setRequestProperty("Accept-Language", "ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3");
            conn.setRequestProperty("Accept-Encoding", "identity");
            
            conn.connect();

            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "cp1251"));
            
            String line;
            String buffer = "";

            while ((line = rd.readLine()) != null) {
                if (line.indexOf("mailto=") > -1){
                    line = line.substring(line.indexOf("mailto=") + 7);
                    if (line.indexOf("'") > 0) line = line.substring(0, line.indexOf("'"));
                    else continue;
                    buffer += line + ";";
                }
                
                if (line.indexOf("Хэллоуин 2012") > -1){
                    line = line.substring(line.indexOf("Хэллоуин 2012"));
                    line = line.substring(line.indexOf("Очков") + 6);
                    line = line.substring(0, line.indexOf("\""));
                    buffer += line;
                }
            }
            
            if (buffer.length() > 0)
                if (buffer.indexOf(";") != buffer.length() - 1)
                    System.out.println(buffer);
            
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
    }

}

Возможность комментировать доступна после регистрации