Jump to content
Daggra

HTML parser

Recommended Posts

Daggra

boa tarde

estou a desenvolver uma aplicaçao que teria o efeito de fazer parse de uma pagina htlm e objectivo seria associar um titulo a um link. agora o meu problema está em associar o titulo a esse link.

eis o meu exemplo

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class main {

public static void main(String[] args) {

Document doc;
try {
doc = Jsoup.connect("http://publico.pt").get();
String title = doc.title();
System.out.println("WebPage : " + title);		

		 Elements article = doc.select("article[class]");

for (Element link : article) {
System.out.println("\nTitulo : " + link.text());
System.out.println("Link : " + link.attr("href")); // não funciona

}
} catch (IOException e) {
e.printStackTrace();
}
}
}

alguem me pode ajudar?

Edited by apocsantos
geshi

Share this post


Link to post
Share on other sites
siul72

Boas,

A questao 'e que os links sao child nodes dos artigos, eis a solucao:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class App {

public static void main(String[] args) {

 Document doc;
 try {
	 doc = Jsoup.connect("http://publico.pt").get();
	 String title = doc.title();
	 System.out.println("WebPage : " + title);

	 Elements articles = doc.select("article[class]");

	 for (Element article : articles) {
		 System.out.println("\nTitulo : " + article.text() );
		 Elements links = article.select("a[href]");
		 for (Element link : links){
			 System.out.println("Link : " + link.attr("href"));

		 }
	 }
 } catch (IOException e) {
	 e.printStackTrace();
 }
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.