错误解析jsoup

时间:2014-01-23 11:41:45

标签: java android parsing jsoup

我无法解析此页面中每篇文章的标题和日期:www.multiplayer.it。 我试着用这种方式:

protected String doInBackground(String... params) {
            try {

                Document doc = Jsoup.connect(BLOG_URL).get();
                Elements nodeBlogStats = doc.select("div.news-col-0"); //per multiplayer.it Elements nodeBlogStats = doc.select("div.news-col-0 h3"); per ftv #comunePartINI > option
                for(Element sezione : nodeBlogStats)
                {
                     Element info = sezione.getElementsByClass("news-box-category").first();


                     String dataarticolo = info.getElementsByClass("news-box-date").first().text();


                     String titolo = info.getElementsByTag("h3").first().text();

                    titoli.add(titolo); 
                    data.add(dataarticolo); 

                }
            } catch (Exception e) {
                // In caso di errore

                Log.e("ERROR", "PARSING ERROR");
            }

我无法理解如何提取这两个数据。

1 个答案:

答案 0 :(得分:0)

尝试使用它:: ---->非常有帮助我通过此代码解决了

private class MyTask extends AsyncTask<Void, Void, ArrayList<String>> {

ArrayList<String> arr_linkText=new ArrayList<String>();

          @Override
          protected ArrayList<String> doInBackground(Void... params) {

            Document doc;
            String linkText = ""; 

            try {
                doc = Jsoup.connect("https://www.google.com/").get();
                 Elements links = doc.getElementsByTag("a");
                 for (Element el : links) { 
                     linkText = el.attr("href");
                     arr_linkText.add(linkText); // add value to ArrayList
                 }
              } catch (IOException e) {
                // TODO Auto-generated catch block
               e.printStackTrace();
             }
            return arr_linkText;     //<< retrun ArrayList from here
          } 


          @Override
          protected void onPostExecute(ArrayList<String> result) {        

          // get all value from result to display in TextView
               TextView textview=(TextView)findViewById(R.id.textView2);
              for (String temp_result : result) {
                System.out.println("links :: "+temp_result);

                           textview.append (temp_result +"\n");
             }
          }
        }