本文介绍了无法用简单的例子JSOUP解析网站数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从使用Android / JSOUP但是我有一点麻烦钉在下跌过程中一个表中提取以下数据。我想我越来越接近能够使用这个我下面提供的code做的 - 但由于某些原因,我还是不能让我的TextView显示任何表数据
P.S。
直播网址可在必要时提供。
来源:
公共类MainActivity延伸活动{TextView的电视;
最终字符串URL =http://exampleurl.com;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 电视=(的TextView)findViewById(R.id.TextView01);
新MyTask()执行(URL);
}私有类MyTask扩展的AsyncTask<弦乐,太虚,字符串> {
ProgressDialog PROG;
字符串title =; @覆盖
在preExecute保护无效(){
PROG =新ProgressDialog(MainActivity.this);
prog.setMessage(加载......);
prog.show();
} @覆盖
保护字符串doInBackground(字符串... PARAMS){
尝试{
。文档的DOC = Jsoup.connect(PARAMS [0])获得();
元素tableElement = doc.getElementsByClass(数据网格)
。第一();
标题= doc.title();
}赶上(IOException异常五){
e.printStackTrace();
}
返回称号;
} @覆盖
保护无效onPostExecute(字符串结果){
super.onPostExecute(结果);
prog.dismiss();
tv.setText(结果);
}
}
}
TABLE:
<表类=数据网格>
<&TBODY GT;< TR>
<第i产品编号:LT; /第i
<第i个姓名和LT; /第i
<第i性别和LT; /第i
<第i个位置和LT; /第i
< / TR> &所述; TR>
&所述; TD>&下;一个href=\"redirector.cfm?ID=a33660a3-aae0-45e3-9703-d59d77717836&page=1&&lname=&fname=\"标题=501207593> 501207593&安培; NBSP;< / A>< / TD>
< TD>&USER1 LT; / TD>
< TD> M&安培; NBSP;< / TD>
< TD>&不明LT; / TD>
< / TR> &所述; TR>
&所述; TD>&下;一个href=\"redirector.cfm?ID=edf524da-8598-450f-9373-da87db8d6c84&page=1&&lname=&fname=\"标题=501302750> 501302750&安培; NBSP;< / A>< / TD>
< TD>&USER2 LT; / TD>
< TD> M&安培; NBSP;< / TD>
< TD>&不明LT; / TD>
< / TR> &所述; TR>
&所述; TD>&下;一个href=\"redirector.cfm?ID=a78abeea-7651-4ac1-bba2-0dcb272c8b77&page=1&&lname=&fname=\"标题=531201804> 531201804&安培; NBSP;< / A>< / TD>
< TD>&USER3 LT; / TD>
< TD> M&安培; NBSP;< / TD>
< TD>&不明LT; / TD>
< / TR> < / TBODY>< /表>
解决方案
我觉得这行
元素TH = doc.select(TR)第一。
代替应写成
元素TH = doc.select(TR)第一();
I'm attempting to extract the following data from a table via Android / JSOUP however I'm having a bit of trouble nailing down the process. I think I'm getting close to being able to do this using the code I've provided below - but for some reason I still cannot get my textview to display any of the table data.
P.S.
Live URL's can be provided if necessary.
SOURCE:
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:
<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>
解决方案
I think that this line
Element th = doc.select("tr").first;
instead it should be written as
Element th = doc.select("tr").first();
这篇关于无法用简单的例子JSOUP解析网站数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!