4

So I have the following html source:

<form action='http://example.com' method='get'>

        <P>Some example text here.</P>
        <input type='text' class='is-input' id='agent_name' name='deviceName' placeholder='Device Name'>
        <input type='hidden' name='p' value='firefox'>
        <input type='hidden' name='email' value='[email protected]'>
        <input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
        <p><input type='submit' class='btn-blue' style='margin-top:15px;' value='Install'></p>
</form>

Unfortunately this html source is saved as a string. I would like to parse it using something like jsoup. and obtain the following String: <input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>

or better yet, only grab the following value: cITBk236gyd56oiY0fhk6lpuo9nt61Va

The problem I'm running into is that:

a) that value: cITBk236gyd56oiY0fhk6lpuo9nt61Va is consistently changing I cannot look for the entire html tag.

So, I am looking for a better way to do this. Here is what I currently have that does not seem to be working:

//tried use thing, but java was angry for some reason
Jsoup.parse(myString);

// so I used this instead. 
org.jsoup.nodes.Document doc = Jsoup.parse(myString);

// in this case I just tried to select the entire tag. Elements
elements = doc.select("<input name=\"k\"
value=\"cITBkdxJTFd56oiY0fhk6lUu8Owt61Va\" type=\"hidden\">");

//yeah this does not seem to work. I assume it's not a string anymorebut a document. Not sure if it 
//would attempt to print anyway.
System.out.println(elements);

so I guess I can't use select, but even if this would work. I was not sure how to place select that part of the tag and place it into a new string.

3
  • If you can add a unique id to the input element that would be great for parsing. If you can't, you can also try using the name attribute to find the element and return its value attribute. Commented May 21, 2013 at 17:37
  • 1
    Did you check out this page? jsoup.org/cookbook/extracting-data/selector-syntax Commented May 21, 2013 at 17:40
  • @SotiriosDelimanolis thanks for your help. I didn't realize Jsoup could do that. After you said that, I googled for the proper syntax. And was able to parse and complete what I needed. I noticed what I did, was an exact answer as another answer response. Thanks again. Commented May 21, 2013 at 18:02

2 Answers 2

7

You can try this way

Document doc = Jsoup.parse(myString);
Elements elements = doc.select("input[name=k]");
System.out.println(elements.attr("value"));

output:

cITBk236gyd56oiY0fhk6lpuo9nt61Va
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I literally just typed that out. and you provided me hte proper answer. thanks.
0

Try this call to select to get the elements:

elements = doc.select("input[name=k][value=cITBkdxJTFd56oiY0fhk6lUu8Owt61Va]")

In this context, elements must be an Elements object. If you need to extract data from elements, you can use one of these (among others, obviously):

elements.html(); // HTML of all elements
elements.text(); // Text contents of all elements
elements.get(i).html(); // HTML of the i-th element
elements.get(i).text(); // Text contents of the i-th element
elements.get(i).attr("value"); // The contents of the "value" attribute of the i-th element

To iterate over elements, you can use any of these:

for(Element element : elements)
    element.html(); // Or whatever you want

for(int i=0;i<elements.size();i++)
    elements.get(i).html(); // Or whatever you want

Jsoup is an excellent library. The select method uses (lightly) modified CSS selectors for document queries. You can check the valid syntax for the method in the Jsoup javadocs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.