为什么 Jsoup 选择器 div#id 在 netbeans 中有效,但在 eclipse ADT 中无效?
why does Jsoup selector div#id work in netbeans but not in eclipse ADT?
以下代码在 netbeans 中运行良好,但在 eclipse ADT 中,doc.select("div#tickertape_eng a").size()
returns 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") 不起作用?如何让它发挥作用?
试试这个:
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
试试这个,
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
以下代码在 netbeans 中运行良好,但在 eclipse ADT 中,doc.select("div#tickertape_eng a").size()
returns 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") 不起作用?如何让它发挥作用?
试试这个:
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
试试这个,
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