2

Desenvolvi o seguinte código para pegar o conteúdo de determinada pagina web:

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Logger logger= Logger.getLogger("org.bonitasoft");

URL url = null;
File file = new File("C:\\Backup\\page.html");

def x = 1;
//while (x = 1){
    url = new URL("http://site");

    BufferedReader inFile = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter outFile = new BufferedWriter(new FileWriter(file));
    String inputLine;

    while ((inputLine = inFile.readLine()) != null) {
        Matcher matcherRamo = Pattern.compile("Ramo:\\s<.strong>\\s.*").matcher(inputLine)
        Matcher matcherNome = Pattern.compile("consulta-associados-item-nome-fantasia").matcher(inputLine)
        Matcher matcherFone = Pattern.compile("<strong>Fone: <.strong>").matcher(inputLine)
        Matcher matcherEmail = Pattern.compile("<strong>Email: <.strong> <a href=\"mailto:.*\" class=\"link\">").matcher(inputLine)
        Matcher matcherProduto = Pattern.compile("<span class=\"float-left\">").matcher(inputLine)
        Matcher matcherSite = Pattern.compile("<strong>Site: <.strong>\\s<a href=.* target=\"_blank\">").matcher(inputLine)

        if (matcherNome.find()){
            logger.info("NOME: "+inputLine.replace("<h3 class=\"consulta-associados-item-nome-fantasia\">", "").replace("</h3>", "").trim())
        }
        if (matcherFone.find()){
            logger.info("TELEFONE: "+inputLine.replace("<strong>Fone: </strong>", "").trim())
        }
        if (matcherEmail.find()){
            logger.info("EMAIL: "+inputLine.replaceFirst("<strong>Email: <.strong> <a href=\"mailto:.*\" class=\"link\">", "").replace("</a>", "").trim())
        }
        if (matcherRamo.find()){
            logger.info("RAMO: "+inputLine.replace("Ramo: </strong> ", "").replace("<strong>", "").trim())
        }
        if (matcherProduto.find()){
            logger.info("PRODUTO: "+inputLine.replace("<span class=\"float-left\">", "").replace("</span>", "").replace("<br>", " | ").trim())
        }
        if (matcherSite.find()){
            logger.info("SITE: "+inputLine.replaceFirst("<strong>Site: <.strong>\\s<a href=.* target=\"_blank\">", "").replace("</a>", "").trim())
        }

        outFile.write(inputLine);
        outFile.newLine();
    }
    //x++
//}
inFile.close();
outFile.flush();
outFile.close();

Ele esta pegando corretamente as informações que preciso porém quando o processo é encerrado, terminou de ler toda a pagina e me retornou o conteúdo filtrado ele começa a fazer novamente a mesma coisa, infinitamente, se puderem me auxiliar agradeço.

7
  • Só por curiosidade, o que é/faz def x = 1;? 7/11/2017 às 13:17
  • 1
    @IgorVenturelli Foi um teste mal sucedido kk
    – R.Santos
    7/11/2017 às 13:19
  • Posta o código completo.. Aí não tem definição de classe nem de método.. Fica difícil de entender hehe 7/11/2017 às 13:21
  • @IgorVenturelli esse é o código completo, não tenho mais nada
    – R.Santos
    7/11/2017 às 13:22
  • 1
    @IgorVenturelli, peço desculpa, esqueci de adicionar a tag groovy, coloquei a de Java por ser extremamente semelhante mas é um script em groovy
    – R.Santos
    7/11/2017 às 13:24

1 Resposta 1

0

Resolvi minha questão da seguinte forma:

import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.logging.Logger;
Logger logger= Logger.getLogger("org.bonitasoft");

URL url = null;
def y = 1
def lista = []
BufferedWriter strW = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\TESTE\\teste.csv"), StandardCharsets.ISO_8859_1));
lista.add("EMPRESA;TELEFONE;EMAIL;RAMO;PRODUTO;SITE\n")

    url = new URL("http://site");
    BufferedReader inFile = new BufferedReader(new InputStreamReader(url.openStream()));
    String inLine;
    if (!inFile.toString().isEmpty()){
        while ((inLine = inFile.readLine()) != null) {
            Matcher matcherRamo = Pattern.compile("Ramo:\\s<.strong>\\s.*").matcher(inLine)
            Matcher matcherNome = Pattern.compile("consulta-associados-item-nome-fantasia").matcher(inLine)
            Matcher matcherFone = Pattern.compile("<strong>Fone: <.strong>").matcher(inLine)
            Matcher matcherEmail = Pattern.compile("<strong>Email: <.strong> <a href=\"mailto:.*\" class=\"link\">").matcher(inLine)
            Matcher matcherProduto = Pattern.compile("<span class=\"float-left\">").matcher(inLine)
            //Matcher matcherServicos = Pattern.compile("<strong>Atividade: <.strong>").matcher(inLine)
            Matcher matcherSite = Pattern.compile("<strong>Site: <.strong>\\s<a href=.* target=\"_blank\">").matcher(inLine)
            Matcher matcherNewLine = Pattern.compile("<!-- .consulta-associados-item-infos-right -->").matcher(inLine)
            if (matcherNome.find()){
                lista.add(inLine.replace("<h3 class=\"consulta-associados-item-nome-fantasia\">", "").replace("</h3>", "").trim()+";")
                //logger.info(inLine.replace("<h3 class=\"consulta-associados-item-nome-fantasia\">", "").replace("</h3>", "").trim()+";")
            }
            if (matcherFone.find()){
                lista.add(inLine.replace("<strong>Fone: </strong>", "").trim()+";")
                //logger.info(inLine.replace("<strong>Fone: </strong>", "").trim()+";")
            }
            if (matcherEmail.find()){
                lista.add(inLine.replaceFirst("<strong>Email: <.strong> <a href=\"mailto:.*\" class=\"link\">", "").replace("</a>", "").trim()+";")
                //logger.info(inLine.replaceFirst("<strong>Email: <.strong> <a href=\"mailto:.*\" class=\"link\">", "").replace("</a>", "").trim()+";")
            }
            if (matcherRamo.find()){
                lista.add(inLine.replace("Ramo: </strong> ", "").replace("<strong>", "").trim()+";")
                //logger.info(inLine.replace("Ramo: </strong> ", "").replace("<strong>", "").trim()+";")
            }
            if (matcherProduto.find()){
                lista.add(inLine.replace("<span class=\"float-left\">", "").replace("</span>", "").replace("<br>", " | ").trim()+";")
                //logger.info(inLine.replace("<span class=\"float-left\">", "").replace("</span>", "").replace("<br>", " | ").trim()+";")
            }
            if (matcherSite.find()){
                lista.add(inLine.replaceFirst("<strong>Site: <.strong>\\s<a href=.* target=\"_blank\">", "").replace("</a>", "").trim()+";")
                //logger.info(inLine.replaceFirst("<strong>Site: <.strong>\\s<a href=.* target=\"_blank\">", "").replace("</a>", "").trim()+";")
            }
            if (matcherNewLine.find()){
                lista.add("\n")
            }
        }
        y++
    }
    inFile.close();

//logger.info(lista.toArray().toString().replace("[", "").replace("]", "").replace(",", ""))
strW.write(lista.toArray().toString().replace("[", "").replace("]", "").replace(",", ""))
strW.close();

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .