문제

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top