1
I have a problem with blibioteca Jsoup, that when I try to make the connection to a certain page, it simply does not return me any value from the connection.
public static void main(String[] args) {
try{
Document doc = Jsoup.connect("/").get();
//Pegando elemento das perguntas
Elements elements = doc.select("a.question-hyperlink");
System.out.println("O titulo da página é: "+doc.title());
//exibindo titulo da pergunta
for(int i = 0; i <elements.size(); i++){
System.out.println(elements.get(i).text());
}
}catch(Exception e){
System.out.println("Erro "+ e);
}
}
By coincidence I tested with Stack Overflow and it gave the same problem.
Return of Netbeans IDE:
Erro: org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=/
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.282s
Finished at: Sun May 15 13:26:41 BRT 2016
Final Memory: 5M/109M
------------------------------------------------------------------------
I don’t know if this influences anything, but the kind of project is Maven.
@EDIT
I was able to solve the problem by adding the following method in the connection.
Document doc = Jsoup.connect("/")
.userAgent("Mozilla").get();
Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!
– Sorack