为什么Jsoup选择器div#id在netbeans中工作但在eclipse ADT中不工作?

时间:2015-02-18 14:36:39

标签: android eclipse netbeans jsoup adt

以下代码在netbeans中工作正常,但在eclipse ADT中,doc.select("div#tickertape_eng a").size()返回0。

try {
            Document doc = Jsoup.connect("http://bdnews24.com").get();
            if (doc != null) {
                Elements links = doc.select("div#tickertape_eng a");
                for (Element link : links) {
                    headlines.add(link.text());
                    headlinks.add(link.absUrl("href"));
                }

            } else {
                Toast.makeText(getActivity(), "Doc not found",
                        Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

在netbeans中,我可以打印link.text()和link.absUrl(" href"),但不能在eclipse中打印。 Eclipse在logcat中打印doc.title(),但为什么doc.select(" div#tickertape_eng a")不起作用?如何使它工作?

2 个答案:

答案 0 :(得分:0)

试试这个:

Document doc = Jsoup.connect("http://bdnews24.com")
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
  .get();

用户代理是服务器猜测您正在使用哪个浏览器的原因。如果它检测到移动浏览器,则可以将您重定向到移动页面。因此,强制使用桌面浏览器用户代理是确保您获得位于bdnews24.com的标准页面的方法。

您可以在此处找到大多数可用的用户代理:http://www.useragentstring.com/pages/useragentstring.php

答案 1 :(得分:0)

试试这个,

Document doc = Jsoup.connect("http://bdnews24.com").get();
Elements div = doc.select("div#tickertape_eng");

   Elements get = div.select("a[href]");

     for (Element elem : get) {

         System.out.println("Link : " +elem.attr("href"));
         System.out.println("Text  : " +elem.text());        
    }
}

<强>打印

Link : http://bdnews24.com/politics/2015/02/18/euro-mps-will-continue-to-urge-bnp-to-sever-ties-with-jamaat-hifazat-says-junior-minister
Text  : Euro MPs will continue to urge BNP to sever ties with Jamaat, Hifazat, says junior minister
Link : http://bdnews24.com/bangladesh/2015/02/18/appellate-division-publishes-full-verdict-on-jamaat-leader-kamaruzzaman
Text  : Appellate Division has published full verdict on Jamaat leader Kamaruzzaman
Link : http://bdnews24.com/bangladesh/2015/02/18/jamaat-e-islami-leader-abdus-subhan-to-hang-for-war-time-atrocities-in-pabna
Text  : Jamaat-e-Islami leader Abdus Subhan to hang for war-time atrocities in Pabna
Link : http://bdnews24.com/bangladesh/2015/02/18/prosecution-happy-with-subhan-verdict-defence-says-will-appeal
Text  : Prosecution happy with Subhan verdict, defence says will appeal
Link : http://bdnews24.com/bangladesh/2015/02/18/un-chief-writes-to-hasina-khaleda-urges-for-talks
Text  : UN Secy-Gen Ban Ki-moon writes to Hasina, Khaleda, urges for talks