gpt4 book ai didi

java - 解析 jsoup 错误。根元素为空

转载 作者:行者123 更新时间:2023-12-01 12:04:50 25 4
gpt4 key购买 nike

我会用jsoup解析这个页面:http://www.verlata.it/eventi在我的 doInBackground() 方法中:

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

try {
errore = false;
final Document doc = Jsoup.connect("http://www.verlata.it/eventi").timeout(30000).get();

getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (doc != null) {
rootElement = doc.body().getElementById("div#container");
} else {
errore = true;
Log.d("errore", "errore parsing 1");
notFound.setVisibility(View.VISIBLE);
notFound.setText("Ops, something went wrong. Maybe the blog is offline or please check your connection.");

}
if (rootElement != null) {
Elements elements = rootElement.getElementsByTag("div#body_content");
for(Element element : elements){
if (!errore) {
String descrizione = element.select("div").text();
String titolo = element.select("h2").text();
//String urldesc = element.select("h2 a").first().attr("abs:href");


titoli.add(titolo);
descrizioni.add(descrizione);
//url.add(urldesc);
} else {
errore = true;
break;
}
}
} else {
errore = true;
Log.d("errore", "errore parsing 2");
notFound.setVisibility(View.VISIBLE);
notFound.setText("Ops, something went wrong. Maybe the blog is offline or please check your connection.");

}
}
});


} catch (Exception e) {
e.printStackTrace();
/*Toast.makeText(FragmentThree.this, "Errore parsing",
Toast.LENGTH_SHORT).show();*/
Log.d("errore", "errore parsing");
notFound.setVisibility(View.VISIBLE);
notFound.setText("Ops, something went wrong. Maybe the blog is offline or please check your connection.");
}


return null;
}

logcat 向我报告:Log.d("errore", "errore parsing 2"); 所以这意味着 rootElement 为 null.. 怎么可能?我需要的是帖子的标题及其描述。

最佳答案

这一行是错误的:

doc.body().getElementById("div#container");

该方法只需要元素的 ID,而不是整个搜索查询。

doc.body().getElementById("container");

如果您想使用完整的搜索查询,请使用:

doc.select("div#container");

这同样适用于这一行:

Elements elements = rootElement.getElementsByTag("div#body_content");

应该是

Elements elements = doc.select("div#body_content");
<小时/>

您的代码将如下所示:

final Document doc = Jsoup.connect("http://www.verlata.it/eventi").timeout(30000).get();
Element container = doc.body().getElementById("container");
Element bodyContent = doc.body().getElementById("body_content");
System.out.println(container);
System.out.println(bodyContent);

关于java - 解析 jsoup 错误。根元素为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27692865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com