简体   繁体   中英

JSoup Select Problems

I'm having trouble selecting links in my html. Here's the html I have:

<div class=first>
    <a href=www.test1.com>test1</a>

    <div class=nope>
        <a href=www.test2.com>test2</a>
        <a href=www.test3.com>test3</a>
        <a href=www.test4.com>test4</a>
    </div>
</div>

What I want to do is pull the URLs:
www.test2.com
www.test3.com
www.test4.com

I have tried a lot of diferent .select and .not combinations but I just can't figure it out. Can anyone point out what it is I'm doing wrong?

String url = "<div class=first><a href=www.test1.com>test1</a>One<div class=nope><a href=www.test2.com>test2</a>Two</div></div><div class=second><a href=www.test3.com>test3</a></div>";
Document doc = Jsoup.parse(url);
Elements divs = doc.select("div a[href]").not(".first.nope a[href]"); 
System.out.println(divs);
Document doc = Jsoup.parse("your html code/url ");
Elements links = doc.select("div.nope a").first();
for (Element link : links) {
System.out.println(link.attr("href"));

I would do it a little different:

  Elements elements = doc.select("div.nope").select("a[href]");

  for (Element element : elements) {
     System.out.println(element.attr("href"));
  }
Elements data=doc.getElementsByClass("nope")

for(Element d:data)
{
    String yourData= d.tagName("href").toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM