gpt4 book ai didi

java - 使用 jsoup 抓取数据

转载 作者:行者123 更新时间:2023-12-01 18:34:14 27 4
gpt4 key购买 nike

所以我正在使用 jsoup 库抓取一些数据。数据被组织在 html 元素表中。我想在 textView 或 listView 中显示相关数据。对于初学者来说,它需要位于 textView 中。当我尝试显示多个表中的信息时,textView 只显示第一个表。我无法集中精力思考这个问题。希望您能帮助我指出我做错了什么。

这是代码


TextView textView;
Button dohvatiStranicu;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView)findViewById(R.id.textView);
textView.setMovementMethod(new ScrollingMovementMethod());
dohvatiStranicu = (Button)findViewById(R.id.getPageButton);


dohvatiStranicu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new dohvatiStranicu().execute();
}
});


}

public class dohvatiStranicu extends AsyncTask<Void,Void,Void>{

StringBuilder stringBuilder;

@Override
protected void onPreExecute() {
stringBuilder = new StringBuilder();
}

@Override
protected Void doInBackground(Void... voids) {

try{
Document doc = Jsoup.connect("https://inf.ffzg.unizg.hr/index.php/hr/studij/diplomski-studij/ispitni-rokovi?fbclid=IwAR0WuLXdooI_0wB8-vVbgZTs89jX-B0eNY0f4wmB9rScqojSqsA2oN-aQ6I").get();
Elements tables = doc.select("table");
for(Element table : tables){
stringBuilder.append("\n\n\n");
stringBuilder.append(parsirajTablicu(table));
}

}catch(Exception e){
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

textView.setText(stringBuilder);

}
}

private static String parsirajTablicu(Element table){
String text = "\n\n\n\n\n";

Element nazivPredmeta = table.selectFirst("p");

Elements naziviRokova = table.select("th");
Elements datumiRokova = table.select("td");
datumiRokova.remove(0);

text += nazivPredmeta.text()+ "\n\n";
text += naziviRokova.get(0).text() + "\n";
text += " " + datumiRokova.get(0).text() + "\n";
text += " " + datumiRokova.get(4).text() + "\n";
text += " " + datumiRokova.get(8).text() + "\n";

text += naziviRokova.get(1).text() + "\n";
text += " " + datumiRokova.get(1).text() + "\n";
text += " " + datumiRokova.get(5).text() + "\n";
text += " " + datumiRokova.get(9).text() + "\n";

text += naziviRokova.get(2).text() + "\n";
text += " " + datumiRokova.get(2).text() + "\n";
text += " " + datumiRokova.get(6).text() + "\n";
text += " " + datumiRokova.get(10).text() + "\n";

text += naziviRokova.get(3).text() + "\n";
text += " " + datumiRokova.get(3).text() + "\n";
text += " " + datumiRokova.get(7).text() + "\n";
text += " " + datumiRokova.get(11).text() + "\n";

return text;
}
}

最佳答案

第二个表没有“th”元素,则 naziviRokova 为 null,并且 nazivPredmeta.text() 会导致 NullPointerException

您还必须小心使用 datumirokova.get(i),因为如果元素不足,它会导致 IndexOutOfBoundsException

作为一般建议,尝试隔离“逻辑”和“android”集成代码,它将允许您更有效地测试代码。

完整示例:

package org.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class SO_60092082 {

private static String parsirajTablicu(Element table){
String text = "\n\n\n\n\n";

Element nazivPredmeta = table.selectFirst("p");

Elements naziviRokova = table.select("th");
Elements datumiRokova = table.select("td");
datumiRokova.remove(0);

if (nazivPredmeta!=null)
text += nazivPredmeta.text()+ "\n\n";

text += safeGetText(naziviRokova, 0) + "\n";
text += " " + safeGetText(datumiRokova, 0) + "\n";
text += " " + safeGetText(datumiRokova, 4) + "\n";
text += " " + safeGetText(datumiRokova, 8) + "\n";

text += safeGetText(naziviRokova, 1) + "\n";
text += " " + safeGetText(datumiRokova, 1) + "\n";
text += " " + safeGetText(datumiRokova, 5) + "\n";
text += " " + safeGetText(datumiRokova, 9) + "\n";

text += safeGetText(naziviRokova, 2) + "\n";
text += " " + safeGetText(datumiRokova, 2) + "\n";
text += " " + safeGetText(datumiRokova, 6) + "\n";
text += " " + safeGetText(datumiRokova, 10) + "\n";

text += safeGetText(naziviRokova, 3) + "\n";
text += " " + safeGetText(datumiRokova, 3) + "\n";
text += " " + safeGetText(datumiRokova, 7) + "\n";
text += " " + safeGetText(datumiRokova, 11) + "\n";

return text;
}

private static String safeGetText(Elements datumiRokova, int i) {
return datumiRokova.size()>i ? datumiRokova.get(i).text() : "";
}

public static void main(String[] args) {

try{
StringBuilder stringBuilder = new StringBuilder();
Document doc = Jsoup.connect("https://inf.ffzg.unizg.hr/index.php/hr/studij/diplomski-studij/ispitni-rokovi?fbclid=IwAR0WuLXdooI_0wB8-vVbgZTs89jX-B0eNY0f4wmB9rScqojSqsA2oN-aQ6I").get();
Elements tables = doc.select("table");
for(Element table : tables){
stringBuilder.append("\n\n\n");
stringBuilder.append(parsirajTablicu(table));
}

System.out.println(stringBuilder);
}catch(Exception e){
e.printStackTrace();
}
}
}

关于java - 使用 jsoup 抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60092082/

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