使用 jsoup 解析 JavaScript [英] Parse JavaScript with jsoup
本文介绍了使用 jsoup 解析 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 HTML
页面中,我想选择 javascript
变量的值.
下面是 HTML
页面的片段:
In an 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>
我的目标是使用 jsoup
从此页面读取变量 key
的值.jsoup
可以吗?如果是,那么如何?
My aim is to read the value of variable key
from this page using jsoup
.
Is it possible with jsoup
? If yes then how?
推荐答案
由于 jsoup 不是 javascript 库,因此您有两种方法可以解决此问题:
Since jsoup isn't a javascript library you have two ways to solve this:
专业:
- 完全支持 Javascript
缺点:
- 其他库/依赖项
专业:
- 不需要额外的库
- 足以完成简单的任务
缺点:
- 不如 javascript 库灵活
这是一个如何使用jsoup和一些手动"代码获取key
的示例:
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
}
输出(使用您的 html 部分):
key="pqRjnA"
pqRjnA
这篇关于使用 jsoup 解析 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文