1

我正在尝试从 HTML 表中抓取 URL,但是每次这样做时,我都会获取 HREF 标题数据而不是 URL - 有没有人可以解决/避免这种情况?

<table class="datagrid">
        <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Sex</th>
            <th>Location</th>
        </tr>

            <tr>
                <td><a href="redirector.cfm?ID=93bd5121-7a3b-4a56-a576-f432e542047a&page=1&&amp;lname=&amp;fname=" title="501207593">501207593&nbsp;</a></td>
                <td>AARON, JUSTIN COLBY&nbsp;</td>
                <td>M&nbsp;</td>
                <td>Facility 1</td>
            </tr>

            <tr>
                <td><a href="redirector.cfm?ID=c5629a92-7113-487c-ba9b-1e62203ab08d&page=1&&amp;lname=&amp;fname=" title="501302750">501302750&nbsp;</a></td>
                <td>AARONSON, CARY HOWARD&nbsp;</td>
                <td>M&nbsp;</td>
                <td>Facility 2</td>
            </tr>

            <tr>
                <td><a href="redirector.cfm?ID=66d01768-5686-44eb-ac6a-16eb783f52d0&page=1&&amp;lname=&amp;fname=" title="501306284">501306284&nbsp;</a></td>
                <td>ABBOTT, LAUREA &nbsp;</td>
                <td>F&nbsp;</td>
                <td>Facility 3</td>
            </tr>

资源:

public class MainActivity extends Activity {

    TextView tv;
    String url = "http://google.com";
    String tr;
    Document doc;

    @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 {
                doc = Jsoup.connect(params[0]).get();
                Element tableElement = doc.select(".datagrid").first();

                Elements tableRows = tableElement.select("tr");
                for (Element row : tableRows) {
                    Elements cells = row.select("td");
                    if (cells.size() > 0) {
                        title = cells.get(0).text() + "; "
                                + cells.get(1).text() + "; "
                                + cells.get(2).text() + "; "
                                + cells.get(3).text();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return title;
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);
        }
    }
}

目前的结果:

501306284;雅培,劳雷亚;F ; 设施 3

期望的结果:

redirector.cfm?ID=66d01768-5686-44eb-ac6a-16eb783f52d0&page=1&&lname=&fname="title="501306284; 雅培,劳雷亚;F ; 设施 3

或者更好……

期望的结果

点击这里了解更多信息(<-URL);雅培,劳雷亚;F ; 设施 3

4

1 回答 1

1

你似乎得到的只是文字

cells.get(0).text()

我认为这就是你想要做的

cells.get(0).child(0).attr("href")

检查此链接中的文档。

于 2013-10-07T21:25:50.797 回答