如何使用JSoup查找元素(HTML解析)?
问题描述:
我知道,在Jsoup当你想找到一个特定的元素,在它的链接,你可以这样做:如何使用JSoup查找元素(HTML解析)?
Document doc = Jsoup.parse(text);
Element links = doc.select("[href]");
然而,这需要在页面所有链接到每一个网站...
但是,如果我有多个链接,我只想检索专门链接到谷歌的链接。例如:
<a href="http://www.google.com">Google</a>
<a href="http://www.bing.com">Bing</a>
<a href="http://www.google.com">Another Google</a>
而我希望它只带有谷歌在它。我试着做这样的事情:
Element links = doc.select("[href=\"http://www.google.com\"]");
但这不起作用......有没有人有建议?
答
有您只需尝试这样做:
Element links = doc.select("[href=http://www.google.com]");
//Or,
Element links = doc.select("a[href=http://www.google.com]");
//Or with the 'attribute contains' form, the most likely to work:
Element links = doc.select("a[href*=google]");
那么该死的......它实际工作...感谢队友 – ZimZim 2012-04-04 10:41:12