用jsoup解析JavaScript [英] Parse JavaScript with jsoup
问题描述
在 HTML
页面中,我想选择 javascript
变量的值。以下是 HTML
页面的小节。
< input id = hidvalvalue =type =hidden>
< form method =poststyle =padding:0px; margin:0px; name =profileautocomplete =off>
< input name =pqRjnAid =pqRjnAvalue =type =hidden>
< script type =text / javascript>
key =pqRjnA;
< / script>
我的目标是读取变量 key $ c $的值c>从这个页面使用
jsoup
。用 jsoup
可能吗?如果是的话,怎么样?
由于jsoup不是JavaScript库,因此您有两种方法可以解决这个问题:
A。使用JavaScript库
-
临: b
$ b- 全面的Javascript支持
-
p>
- 其他库/附属文件
B。使用Jsoup +手动解析
-
亲:
- 不需要额外的库
- 适用于简单任务
不像JavaScript库那么灵活
$ b
下面是一个如何获取键的例子
使用jsoupand manual代码:
Document doc = ...
元素script = doc.select(script)。first(); //获取脚本部分
模式p = Pattern.compile((?is)key = \(。+?)\); //关键字
的正则表达式Matcher m = p.matcher(script.html()); //你必须在这里使用html而不是文本! (m.find())
{
System.out.println(m.group());文本将放弃'key'部分
; //整个键('key = value')
System.out.println(m.group(1)); // value only
}
输出(使用您的html部分):
key =pqRjnA
pqRjnA
In a HTML
page, i want to pick the value of a javascript
variable. Below is the snippet of HTML
page.
<input id="hidval" value="" type="hidden">
<form method="post" style="padding: 0px;margin: 0px;" name="profile" autocomplete="off">
<input name="pqRjnA" id="pqRjnA" value="" type="hidden">
<script type="text/javascript">
key="pqRjnA";
</script>
My aim is to read the value of variable key
from this page using jsoup
. Is it possible with jsoup
? if yes then how?
Since jsoup isn't a javascript library you have two ways to solve this:
A. Use a javascript library
Pro:
- Full Javascript support
Con:
- Additional libraray / dependencies
B. Use Jsoup + manual parsing
Pro:
- No extra libraries required
- Enough for simple tasks
Con:
- Not as flexible as a javascript library
Here's an example how to get the key
with jsoupand some "manual" code:
Document doc = ...
Element script = doc.select("script").first(); // Get the script part
Pattern p = Pattern.compile("(?is)key=\"(.+?)\""); // Regex for the value of the key
Matcher m = p.matcher(script.html()); // you have to use html here and NOT text! Text will drop the 'key' part
while( m.find() )
{
System.out.println(m.group()); // the whole key ('key = value')
System.out.println(m.group(1)); // value only
}
Output (using your html part):
key="pqRjnA"
pqRjnA
这篇关于用jsoup解析JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!