Queridos estou querendo pegar as imgens no site da receita, consigo pegar as imagens de lá, mas nao consigo o captchia.
sabem me dizer se isso é possivel? pegar tambem o catchia?
[code]
import java.io.;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.URL;
public class DownloadImages {
//The url of the website. This is just an example
private static final String webSiteURL = "http://www.receita.fazenda.gov.br/aplicacoes/atcta/cpf/ConsultaPublica.asp";
//The path of the folder that you want to save the images to
private static final String folderPath = "C:/OPT/";
public static void main(String[] args) {
try {
//Connect to the website and get the html
Document doc = Jsoup.connect(webSiteURL).get();
//Get all elements with img tag ,
Elements img = doc.getElementsByTag("img");
for (Element el : img) {
//for each element get the srs url
String src = el.absUrl("src");
System.out.println("Image Found!");
System.out.println("src attribute is : "+src);
getImages(src);
}
} catch (IOException ex) {
System.err.println("There was an error");
Logger.getLogger(DownloadImages.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void getImages(String src) throws IOException {
String folder = null;
//Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
System.out.println(name);
//Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream( folderPath+ name));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
}[/code]
agradeço desde já a ajuda!
package sys.util.receita;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import sys.util.Const;
import sys.util.texto.Texto;
public class ConsultaCPF {
private Map<String, String> cookies = new HashMap<String, String>();
private String imgSrc;
private String viewState = "";
private String captcha = "";
private String cpf = "";
private String nomeCompleto = "";
private String situacaoCadastral = "";
private String digitoVerificador = "";
public String getCaptcha() {
return captcha;
}
public String getCpf() {
return cpf;
}
public String getNomeCompleto() {
return nomeCompleto;
}
public String getDigitoVerificador() {
return digitoVerificador;
}
public String getSituacaoCadastral() {
return situacaoCadastral;
}
public String getViewState() {
return viewState;
}
public String getImgSrc() {
return imgSrc;
}
public void setCaptcha(String captcha) {
this.captcha = captcha;
}
public void setCpf(String cpf) {
this.cpf = Texto.manterNumeros(cpf);;
}
public boolean init() {
try {
clearValues();
Response response = Jsoup.connect("http://www.receita.fazenda.gov.br/Aplicacoes/ATCTA/CPF/ConsultaPublica.asp")
.method(Method.POST)
.execute();
cookies = response.cookies();
Document document = response.parse();
Element img = document.select("#imgcaptcha").first();
this.imgSrc = "http://www.receita.fazenda.gov.br" + img.attr("src");
Element elemetos1 = document.select("#viewstate").first();
this.viewState = elemetos1.attr("value");
return (!this.imgSrc.equals("") && !this.viewState.equals(""));
} catch (IOException ex) {
return false;
}
}
private void clearValues() {
this.nomeCompleto = "";
this.situacaoCadastral = "";
this.digitoVerificador = "";
this.cpf = "";
this.captcha = "";
this.viewState = "";
this.imgSrc = "";
}
public boolean consultar() throws Exception {
try {
Map<String, String> query = new HashMap<String, String>();
query.put("captcha", this.captcha.trim());
query.put("txtCPF", this.cpf.trim());
query.put("viewstate", this.viewState.trim());
query.put("origem", "comprovante");
query.put("search_type", "CPF");
query.put("captchaAudio", "");
query.put("Enviar", "Consultar");
Connection.Response resposta;
resposta = Jsoup.connect("http://www.receita.fazenda.gov.br/aplicacoes/atcta/cpf/ConsultaPublicaExibir.asp")
.data(query)
.cookies(cookies)
.method(Connection.Method.POST)
.execute();
Document document = resposta.parse();
document.outputSettings().charset(Const.DEFAULT_CHARSET);
Elements elements = document.getElementsByClass("clConteudoDados");
for (Element element : elements) {
if(element.text().contains("Nome da Pessoa Física:")) {
this.nomeCompleto = Texto.trocarCaracteresAcentuados(element.text().replace("Nome da Pessoa Física:", "").trim().toUpperCase());
}
if(element.text().contains("Situação Cadastral:")) {
this.situacaoCadastral = Texto.trocarCaracteresAcentuados(element.text().replace("Situação Cadastral:", "").trim().toUpperCase());
}
if(element.text().contains("Digito Verificador:")) {
this.digitoVerificador = element.text().replace("Digito Verificador:", "").trim();
}
}
return (!this.nomeCompleto.equals(""));
} catch (IOException ex) {
return false;
}
}
}
Até estes dias funcionava isso, agora parou.
Foi mudado o esquema do captcha e envio das informações…