Jsoup从Li获取超链接 [英] Jsoup getting a hyperlink from li

查看:76
本文介绍了Jsoup从Li获取超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

<div id = top1>
   <div id topsub1>
   <ul class="student">
   <li>
    <a href="/thomas">Tom</a>
    </li>
    </div>
   <div id topsub2>
   <ul class="student">
   <li>
    <a href="/thomas1">Tom1</a>
    </li>
    </div>
 </div>

我想获取href和文本 所以我做了 元素xx =选择(div div ul li)

I want to get the href and text So I did Elements xx= select (div div ul li)

当我为y做foreach xx时,如果我这样做

when I do foreach xx for y and if I do

string1 = y.text(); //正在打印汤姆 String2 = y.attr("href")//始终为空.我无法得到/托马斯? 我也尝试过y.attr("a [href]"))

string1= y.text(); //This is printing Tom String2= y.attr("href") //This is always empty. I am not able to get /thomas? I also tried y.attr("a[href]"))

还有什么是: doc.select(.studentnames> a");? 这是否意味着在ID = studentnames上获得所有正确的"a" ??

and also what is : doc.select(".studentnames > a");? does this mean that on the ID=studentnames get all the "a" correct???

推荐答案

如果您只是简单地Elements eles = doc.select("a[href]");怎么办?另外,我相信使用attrib(...)方法时,您不会传递标记,而只是传递属性名称本身.

What if you simply did Elements eles = doc.select("a[href]");? Also I believe that when using the attrib(...) method, you don't pass in the tag, just the attribute name itself.


您声明:

执行doc.select("a [href] ..有很多属性和hrefs ..我可以得到-学生姓名和他的网站

the issue with doing doc.select("a[href]..there are many many attributes and hrefs..i want only that is next to student name..thats why i am doing immediately after I do text so that I can get - name of student and his website

然后通过依次调用多个选择或将它们链接在一起来使选择返回的优化结果:

Then refine result returned by your select by calling multiple selects sequentially or chaining them:

Elements eles = doc.select("ul.student").select("a[href]");

或者(我从未做过):

Elements eles = doc.select("ul.student a[href]");

甚至:

Elements eles = doc.select("div > div > ul.student > li > a[href]");

这篇关于Jsoup从Li获取超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆