adb*_*ads 3 java html-parsing jsoup
所以我有以下html源代码:
<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='example@example.com'>
<input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
<p><input type='submit' class='btn-blue' style='margin-top:15px;' value='Install'></p>
</form>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个html源代码保存为字符串.我想用jsoup之类的东西来解析它.并获取以下字符串:
<input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
或者更好的是,只获取以下值: cITBk236gyd56oiY0fhk6lpuo9nt61Va
我遇到的问题是:
a)该值:cITBk236gyd56oiY0fhk6lpuo9nt61Va一直在变化我无法找到整个html标签.
所以,我正在寻找一种更好的方法来做到这一点.以下是我目前看来不起作用的内容:
//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);
Run Code Online (Sandbox Code Playgroud)
所以我想我不能使用select,但即使这样也行.我不知道如何选择标签的那一部分并将其放入一个新的字符串中.
你可以试试这种方式
Document doc = Jsoup.parse(myString);
Elements elements = doc.select("input[name=k]");
System.out.println(elements.attr("value"));
Run Code Online (Sandbox Code Playgroud)
输出:
cITBk236gyd56oiY0fhk6lpuo9nt61Va
Run Code Online (Sandbox Code Playgroud)