Olá, estou com um probleminha aqui;
Meu objetivo é individualizar valores retornado por uma String que captura o código fonte uma página qualquer por meio do código abaixo:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.*;
public class HttpClientTutorial {
private static String url = "http://br.finance.yahoo.com/q?s=PETR4.SA";
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
Eu estava tentando da seguinte maneira:
String html = "<small><span id='yfs_t10_petr4.sa'></span>"; //define a parte do código em que se deseja capturar
Pattern p = Pattern.compile("<.*?>"); //acessa o que tive entre os sinais <>
Matcher m = p.matcher(html);
String semHtml = m.replaceAll("");
System.out.println(semHtml);
Só que o problema é que em uma String não consigo retornar os valores desejados;
Muito obrigado, toda ajuda é bem vinda..