0

我正在使用以下代码从段落标记中选择数据(因为它跳过了粗体标记中的数据),并且工作正常。

public class MyTask extends AsyncTask<String, Void, String>{

        public String data;
        public String url;

        @Override
    protected String doInBackground(String ... params){

        data = "";
        url = params[0];       

        try{
             Document doc = Jsoup.connect(url).get();
             Elements e = doc.select("p");
             for (Element element : e){
                  if(element!=null){
                       data+=element.ownText();
                       data+='\n';
                       data+='\n';
                  }
             }
        } 
        catch(Exception e){
             // print stack trace
        }
        return data;
    }

我必须从段落标签中选择数据,它也包含粗体标签。现在,我怎样才能选择整个数据,而不跳过粗体标签的数据(段落和粗体标签的数据)。

4

2 回答 2

0

源不是有效的 HTML。

要获得您想要的内容,您必须选择<body>标签并检索它的文本。

    String data = "";
    Document doc = Jsoup.connect("http://www.nahjulbalagha.org/SermonDetail.php?Sermon=1").get();
    Element e = doc.select("body").first();
    data += e.text();
于 2013-10-11T12:33:28.210 回答
0

使用 Jsoup 你必须知道,(你的结果)是一个 DOM 文档。你的标签

层次较低,是表数据的一部分"

<td> <!-- InstanceBeginEditable name="EditRegion1" --> 

                <b>Creation of Earth and Sky and the birth of Adam. </b><hr><br><p>Praise is due to Allah whose worth cannot be described by speakers, whose bounties cannot be counted by calculators and whose claim (to obedience) cannot be satisfied by those who attempt to do so, whom the height of intellectual courage cannot appreciate, and the divings of understanding cannot reach; He for whose description no limit has been laid down, no eulogy exists, no time is ordained and no duration is fixed.<p>

He brought forth creation through His Omnipotence, dispersed winds through His Compassion, and made firm the shaking earth with rocks. <p>"

,因此,如果您还想拥有 b 标签值和 p 标签值,请选择标签 td :

Elements e = doc.select("td"); 

谢谢

于 2013-10-11T13:14:58.367 回答