首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用简单的JSOUP示例解析网站表数据

无法使用简单的JSOUP示例解析网站表数据
EN

Stack Overflow用户
提问于 2013-10-02 01:43:03
回答 2查看 2.8K关注 0票数 0

我正在尝试通过Android / JSOUP从一个表中提取以下数据,但是我在确定这个过程时遇到了一些麻烦。我想我已经能够使用下面提供的代码来实现这一点了--但是由于某些原因,我仍然无法让我的文本视图显示任何表数据。

附注:

如有必要,可提供实时URL。

来源:

代码语言:javascript
运行
复制
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);
    }
}
}

表:

代码语言:javascript
运行
复制
<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&amp;page=1&amp;&amp;lname=&amp;fname=" title="501207593">501207593&nbsp;</a></td>
                <td>USER1</td>
                <td>M&nbsp;</td>
                <td>Unknown</td>
            </tr>

            <tr>
                <td><a href="redirector.cfm?ID=edf524da-8598-450f-9373-da87db8d6c84&amp;page=1&amp;&amp;lname=&amp;fname=" title="501302750">501302750&nbsp;</a></td>
                <td>USER2</td>
                <td>M&nbsp;</td>
                <td>Unknown</td>
            </tr>

            <tr>
                <td><a href="redirector.cfm?ID=a78abeea-7651-4ac1-bba2-0dcb272c8b77&amp;page=1&amp;&amp;lname=&amp;fname=" title="531201804">531201804&nbsp;</a></td>
                <td>USER3</td>
                <td>M&nbsp;</td>
                <td>Unknown</td>
            </tr>

    </tbody></table>
EN

回答 2

Stack Overflow用户

发布于 2013-10-02 05:46:21

您的表没有标题。尝试使用Jsoup在TextView中获取一些文本:

代码语言:javascript
运行
复制
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();
} 
票数 0
EN

Stack Overflow用户

发布于 2013-11-08 05:32:25

我觉得这条线

代码语言:javascript
运行
复制
Element th = doc.select("tr").first;

相反,它应该写成

代码语言:javascript
运行
复制
Element th = doc.select("tr").first(); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19122596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档