简体   繁体   中英

Unable to Use Simple JSOUP Example To Parse Website Table Data

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.

PS

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&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>

Your table doesn't have title. Try this to get some text in TextView with Jsoup:

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();
} 

I think that this line

Element th = doc.select("tr").first;

instead it should be written as

Element th = doc.select("tr").first(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM