Jsoup parsing HTML issue

2019-09-19 07:07发布

问题:

I am new to Jsoup and am trying to parse a website, with the following html, and retrieve the value of the text input in the html below, specifically the "value=14" which I then want to display that value (the number 14 in this case) as a string in a text view in my android app. I have tried multiple ways but it hasn't worked, I just receive "null". Please show example.

<div id="PatientsCurrentlyInClinic" style="display: none"> <!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

            <h3>Which clinic are you updating?</h3>
            <form action="" method="get">
            <p>
                <select name="patientclinicid" id="patientclinicid"><option value="2" selected>Location Two</option><option value="1">Location One</option><option value="3">Location Three</option></select>               </p>



                <h4>How many patients are in the clinic?</h4>
                <p>
                    To provide better service to your patients, please enter the current number of patients in your clinic.
                </p>
                    <input class="text-input medium-input" type="text" id="small-input" name="patientsInClinic" value="14"/>

                    <p><input class="button" name="patients-clinic" type="submit" value="Update" /></p>


            </form>

        </div> <!-- End #messages -->

My attempt that gives me "null" is as follows:

private class Title extends AsyncTask<Void, Void, Void> {
    String name;
    String value;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(HTML.this);
        mProgressDialog.setTitle("Checking Database");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {

            Document doc = Jsoup.connect(url).get();
            Elements inputElems =doc.select("input#small-input");
            for (Element inputElem : inputElems){
                name = inputElem.attr("name");
                value = inputElem.attr("value");
            }
        } catch(Throwable t) {
            t.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set title into TextView
        TextView txttitle = (TextView) findViewById(R.id.showPatientNumber);
        txttitle.setText(value);
        mProgressDialog.dismiss();
    }
}

回答1:

try this:

 Elements inputElems =doc.select("input");
 for (Element inputElem : inputElems){
    name = inputElem.attr("name").first();
    value = inputElem.attr("value").first();
 }


回答2:

I was able to finally obtain the value by using the following code:

Element pInput = doc.select("input[id=small-input]").first();
           rPts = pInput.attr("value");

This came up null initially but after I added the following code to obtain an authentication cookie which I then passed to the next web page where my data value was stored and from where I was trying to get the parse data from, it was successful.

  Document doc1 = res.parse();
            String sessionId = res.cookie("SESSIONID");



            Document doc = Jsoup.connect(url)
                    .cookie("SESSIONID", sessionId)
                    .get();


回答3:

try this,

Elements inputElems =doc.select("input");
Iterator<Element> linksIt = inputElems .iterator();

while (linksIt.hasNext()) {
    Element inputElem = linksIt.next();
    String id = inputElem.attr("id");

    if(id.equals("small-input")){
        name = inputElem.attr("name");
        value= inputElem.attr("value");
    }
}