Buscador de html

8 respostas
mimih07

preciso que esse programa salva no diretorio ou o usuario informe onde salvar e um contador para que fique salvando durante o tempo que o usuario digitar.
estou precisando de ideias e sujestoes
favor me ajude.

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.*;

//Crawler

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;
public class Prototipo extends JFrame implements ActionListener

{

JTextArea TA;

JTextField TF,TF1;

JButton B1, B2;

JPanel P1,P2;

int posInicial= 0;
public static void main (String args[])
{
	JFrame Janela = new Prototipo();
	Janela.show();
}

Prototipo()
{
	//Janela
	setTitle("Busca HTML");
	setLocation(100,50);
	setSize(600,600);
	setResizable(false);
	getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

	TF = new JTextField(("http://"),20);
	TF1= new JTextField(10);

	TA = new JTextArea(30,50);
    JScrollPane s1 = new JScrollPane(TA);

	//Botões
	B1 = new JButton("Pesquisa");
	B1.addActionListener(this);
	B2 = new JButton("Selecionar");
	B2.addActionListener(this);

	//Area de Texto
	P1 = new JPanel();
	P1.setLayout(new GridLayout(2,2));
	P1.setBackground(new Color(200,200,200));
	P2 = new JPanel();
	P2.setLayout(new FlowLayout(FlowLayout.CENTER));
	P2.setBackground(new Color(200,200,200));

	P1.add(TF);		P1.add(B1);
	P1.add(TF1);	P1.add(B2);
	P2.add(s1);

	getContentPane().add(P1);
	getContentPane().add(P2);

}

public void actionPerformed(ActionEvent e)
{

	if (e.getSource()==B1)
	{
		//Implementação Crawler --> Busca Site e Copia o Código HTML
		try
		{
			URL minha_url = new URL (TF.getText());
			BufferedReader dado = new BufferedReader( new InputStreamReader( minha_url.openStream()));
			String strTemp = "";
			while( null != (strTemp = dado.readLine()))
			{
				TA.append(strTemp+("\n"));
			}
		}
		catch (Exception ex)
		{
         ex.printStackTrace();
		}
	}

	if (e.getSource()==B2)
	{
		//Parser --> Encotrar Palavras no HTML
		String pesquisa = TF1.getText();
		               int res = TA.getText().indexOf(pesquisa, posInicial);

		               if(res < 0)
		               {
		                  JOptionPane.showMessageDialog(null, "Texto não encontrado");
		                  posInicial = 0;
		               }
		               else
		               {
		                  TA.requestFocus();
		                  TA.select(res, res + pesquisa.length());
		                  posInicial = res + pesquisa.length();
		               }
	 }
}

}

8 Respostas

natureza

Edita seu tópico e coloca dentro da tag code.

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
//Crawler 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 

public class Prototipo extends JFrame implements ActionListener 
{ 
JTextArea TA; 
JTextField TF,TF1; 
JButton B1, B2; 
JPanel P1,P2; 
int posInicial= 0; 

public static void main (String args[]) 
{ 
JFrame Janela = new Prototipo(); 
Janela.show(); 
} 

Prototipo() 
{ 
//Janela 
setTitle("Busca HTML"); 
setLocation(100,50); 
setSize(600,600); 
setResizable(false); 
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER)); 

TF = new JTextField(("http://"),20); 
TF1= new JTextField(10); 

TA = new JTextArea(30,50); 
JScrollPane s1 = new JScrollPane(TA); 

//Botões 
B1 = new JButton("Pesquisa"); 
B1.addActionListener(this); 
B2 = new JButton("Selecionar"); 
B2.addActionListener(this); 

//Area de Texto 
P1 = new JPanel(); 
P1.setLayout(new GridLayout(2,2)); 
P1.setBackground(new Color(200,200,200)); 
P2 = new JPanel(); 
P2.setLayout(new FlowLayout(FlowLayout.CENTER)); 
P2.setBackground(new Color(200,200,200)); 

P1.add(TF); P1.add(B1); 
P1.add(TF1); P1.add(B2); 
P2.add(s1); 

getContentPane().add(P1); 
getContentPane().add(P2); 

} 

public void actionPerformed(ActionEvent e) 
{ 

if (e.getSource()==B1) 
{ 
//Implementação Crawler --> Busca Site e Copia o Código HTML 
try 
{ 
URL minha_url = new URL (TF.getText()); 
BufferedReader dado = new BufferedReader( new InputStreamReader( minha_url.openStream())); 
String strTemp = ""; 
while( null != (strTemp = dado.readLine())) 
{ 
TA.append(strTemp+("\n")); 
} 
} 
catch (Exception ex) 
{ 
ex.printStackTrace(); 
} 
} 

if (e.getSource()==B2) 
{ 
//Parser --> Encotrar Palavras no HTML 
String pesquisa = TF1.getText(); 
int res = TA.getText().indexOf(pesquisa, posInicial); 

if(res < 0) 
{ 
JOptionPane.showMessageDialog(null, "Texto não encontrado"); 
posInicial = 0; 
} 
else 
{ 
TA.requestFocus(); 
TA.select(res, res + pesquisa.length()); 
posInicial = res + pesquisa.length(); 
} 
} 
} 
}

Inté !

zoren

Quando a sua pesquisa terminar pegue o que está no texto área e salve num arquivo, ai vc cria um arquivo e coloca o conteudo lá

quanto ao caminho do arquivo é só especificar na interface

e ao tempo vc vai ter q usar uma thread

mimih07

axo q pelo q entendi eh assim mais ta dando erro, ajuda por favor

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.<em>;

import <a href="http://java.io">java.io</a>.</em>;

//Crawler

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

public class Prototipo extends JFrame implements ActionListener

{

JTextArea TA;

JTextField TF,TF1;

JButton B1, B2;

JPanel P1,P2;

// TextArea TA1;

int posInicial= 0;

public static void main (String args[])

{

JFrame Janela = new Prototipo();

Janela.show();

}

Prototipo()

{

//Janela

setTitle(Busca HTML);

setLocation(100,50);

setSize(600,600);

setResizable(false);

getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

TF = new JTextField((http://”),20);

TF1= new JTextField(10);

TA = new JTextArea(30,50);

JScrollPane s1 = new JScrollPane(TA);

//Botões

B1 = new JButton(Pesquisa);

B1.addActionListener(this);

B2 = new JButton(Selecionar);

B2.addActionListener(this);

//Area de Texto

P1 = new JPanel();

P1.setLayout(new GridLayout(2,2));

P1.setBackground(new Color(200,200,200));

P2 = new JPanel();

P2.setLayout(new FlowLayout(FlowLayout.CENTER));

P2.setBackground(new Color(200,200,200));

P1.add(TF);  P1.add(B1);

P1.add(TF1); P1.add(B2);

P2.add(s1);

getContentPane().add(P1);

getContentPane().add(P2);

}

public void actionPerformed(ActionEvent e)

{

String prototipo = c:/arq.txt;

if (e.getSource()==B1)

{

//Implementação Crawler --> Busca Site e Copia o Código HTML

try

{
String s = TA.getText();
byte b[] = s.getBytes();
FileOutputStream out = new FileOutputStream (prototipo);
out.write(b);
out.close();

}
try
{

URL minha_url = new URL (TF.getText());
BufferedReader dado = new BufferedReader( new InputStreamReader( minha_url.openStream()));
String strTemp = "";
while( null != (strTemp = dado.readLine()))


{
 TA.append(strTemp+("\n"));
}
}

catch (Exception ex)

{

ex.printStackTrace();

}

}

if (e.getSource()==B2)

{

try
      {
  FileInputStream in = new FileInputStream (prototipo);
  byte bt [10240];
  in.read (bt);
  String  S = new String (bt);
  TA.setText (s);
  in.close();
      }
//Parser --> Encotrar Palavras no HTML

String pesquisa = TF1.getText();

int res = TA.getText().indexOf(pesquisa, posInicial);

if(res < 0)

{

JOptionPane.showMessageDialog(null, Texto não encontrado);

posInicial = 0;

}

else

{

TA.requestFocus();

TA.select(res, res + pesquisa.length());

posInicial = res + pesquisa.length();

}

}

}

}
zoren

calma, eu te ajudo, mas qual é o erro que está dando??

no seu código tem alguns erros, tipo a declaração do vetor de bytes

me explica o que vc quer salvar no arquivo, é o código fonte da página todo??

mimih07

e sim é pra salvar o codigo html

zoren

Do mesmo jeito que vc fez pra pegar o html que gera a página vc vai fazer pra ewscrever no arquivo

mas sm usar buffered reader

com o input Stream vc vai ler os bytes e escrever num arquivo com o FileOutputStream

mimih07

oh os erros q da

E:\Meus Documentos\Downloads\busca(2).java:58: try without catch or finally

try

^

E:\Meus Documentos\Downloads\busca(2).java:92: ] expected

byte bt [10240];

^

E:\Meus Documentos\Downloads\busca(2).java:92: illegal start of expression

byte bt [10240];

^

E:\Meus Documentos\Downloads\busca(2).java:89: try without catch or finally

try

^

4 errors

Ferramenta concluída com código de saída 1
:cry:

zoren

todo bloco try tem q ter no minimo um bloco catch ou finally

ex:

try {
     //algo que pode dar erro
} catch (Exception erro) {
      erro.printStackTrace();
}

quanto ao seu vetor de bytes ele está declarado de uma maneira incorreta

faz assim ó

byte[] bt = new byte[10240];
Criado 29 de outubro de 2009
Ultima resposta 29 de out. de 2009
Respostas 8
Participantes 3