[英]Unable to Use Simple JSOUP Example To Parse Website Table Data
我試圖通過Android / JSOUP從表中提取以下數據,但是在確定該過程時遇到了一些麻煩。 我想我已經接近使用下面提供的代碼來執行此操作了-但是由於某些原因,我仍然無法獲得textview來顯示任何表數據。
聚苯乙烯
如有必要,可以提供實時URL。
資源:
public class MainActivity extends Activity {
TextView tv;
final String URL = "http://exampleurl.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.getElementsByClass("datagrid")
.first();
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
表:
<table class="datagrid">
<tbody><tr>
<th>Item No.</th>
<th>Name</th>
<th>Sex</th>
<th>Location</th>
</tr>
<tr>
<td><a href="redirector.cfm?ID=a33660a3-aae0-45e3-9703-d59d77717836&page=1&&lname=&fname=" title="501207593">501207593 </a></td>
<td>USER1</td>
<td>M </td>
<td>Unknown</td>
</tr>
<tr>
<td><a href="redirector.cfm?ID=edf524da-8598-450f-9373-da87db8d6c84&page=1&&lname=&fname=" title="501302750">501302750 </a></td>
<td>USER2</td>
<td>M </td>
<td>Unknown</td>
</tr>
<tr>
<td><a href="redirector.cfm?ID=a78abeea-7651-4ac1-bba2-0dcb272c8b77&page=1&&lname=&fname=" title="531201804">531201804 </a></td>
<td>USER3</td>
<td>M </td>
<td>Unknown</td>
</tr>
</tbody></table>
您的表格沒有標題。 嘗試使用Jsoup在TextView中獲取一些文本:
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid");
Element th = doc.select("tr").first;
Element firstTh = th.select("th").first();
title = firstTh.text();
}
我認為這條線
Element th = doc.select("tr").first;
相反,它應該寫成
Element th = doc.select("tr").first();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.