如何在 pharo/smalltalk 中提取和存储服务器响应返回的 key/value
How can I extract and store and key/value returned by a server response in pharo/smalltalk
在我的 ZnClient 请求中,我从服务器得到了一个 HTML 格式的响应,其中包含我想提取和存储以备后用的键和值。我如何在 Pharo 中执行此操作?
<div id="login_block">
<div class="text-center"><img src="/static/img/logo-mool2016.black.7272bc78ba54.png" width="223" alt=LOGO" onclick="showChooseLogin();"></div>
<h3 class="text-center">Connectez-vous pour accéder à <span class="product-name">Tool Platform</span></h3>
<div id="login_choosen" class="login_block ui-widget-content ui-corner-all">
<form method="post" action="." id="login_form"><input type='hidden' name='csrfmiddlewaretoken' value='fLTzkLA7yhy7YKDvohM0PJstFJJCEk2JinfjOyzCe2NA495QKznLgO1wzi64P2S8' />
<p><label for="id_email">Email :</label> <input class="login" id="id_email" maxlength="75" name="email" type="text" required /></p>
<p><label for="id_password">Password :</label> <input class="login" id="id_password" name="password" type="password" required /></p>
<button type="submit" class="btn btn-connect pull-right">Connexion</button>
</form>
</div>
</div>
您可以使用 HTML 解析器提取此信息,例如 Soup。
这是一个精简的工作示例:
|content soup dict|
content := '
<div>
<form method="post" action="." id="login_form">
<input type="hidden" name="csrfmiddlewaretoken" value="***special***" />
<input class="login" id="id_email" name="email" type="text" required />
</form>
</div>'.
dict := Dictionary new.
soup := Soup fromString: content.
(soup findAllTags: 'input') do: [ :each |
dict at: (each attributeAt: 'name') put: (each attributeAt: 'value') ].
dict
现在包含以下内容:
'csrfmiddlewaretoken'->'***special***'
'email'->nil
您可以从 Pharo 目录加载 XMLParserHTML
and XPath
。那么这应该可以解决问题:
| xPath htmlDoc inputs input |
"match inputs with token value of the name attrite"
xPath := '//input[@name="csrfmiddlewaretoken"]' asXPath.
"parse your html"
htmlDoc := (XMLHTMLParser on: htmlString) parseDocument.
"match all inputs with the token"
inputs := xPath in: htmlDoc.
"assuming there is only 1 element like that"
input := inputs first.
"get the value attribute from the element"
^ input attributeAt: 'value'.
在我的 ZnClient 请求中,我从服务器得到了一个 HTML 格式的响应,其中包含我想提取和存储以备后用的键和值。我如何在 Pharo 中执行此操作?
<div id="login_block">
<div class="text-center"><img src="/static/img/logo-mool2016.black.7272bc78ba54.png" width="223" alt=LOGO" onclick="showChooseLogin();"></div>
<h3 class="text-center">Connectez-vous pour accéder à <span class="product-name">Tool Platform</span></h3>
<div id="login_choosen" class="login_block ui-widget-content ui-corner-all">
<form method="post" action="." id="login_form"><input type='hidden' name='csrfmiddlewaretoken' value='fLTzkLA7yhy7YKDvohM0PJstFJJCEk2JinfjOyzCe2NA495QKznLgO1wzi64P2S8' />
<p><label for="id_email">Email :</label> <input class="login" id="id_email" maxlength="75" name="email" type="text" required /></p>
<p><label for="id_password">Password :</label> <input class="login" id="id_password" name="password" type="password" required /></p>
<button type="submit" class="btn btn-connect pull-right">Connexion</button>
</form>
</div>
</div>
您可以使用 HTML 解析器提取此信息,例如 Soup。
这是一个精简的工作示例:
|content soup dict|
content := '
<div>
<form method="post" action="." id="login_form">
<input type="hidden" name="csrfmiddlewaretoken" value="***special***" />
<input class="login" id="id_email" name="email" type="text" required />
</form>
</div>'.
dict := Dictionary new.
soup := Soup fromString: content.
(soup findAllTags: 'input') do: [ :each |
dict at: (each attributeAt: 'name') put: (each attributeAt: 'value') ].
dict
现在包含以下内容:
'csrfmiddlewaretoken'->'***special***'
'email'->nil
您可以从 Pharo 目录加载 XMLParserHTML
and XPath
。那么这应该可以解决问题:
| xPath htmlDoc inputs input |
"match inputs with token value of the name attrite"
xPath := '//input[@name="csrfmiddlewaretoken"]' asXPath.
"parse your html"
htmlDoc := (XMLHTMLParser on: htmlString) parseDocument.
"match all inputs with the token"
inputs := xPath in: htmlDoc.
"assuming there is only 1 element like that"
input := inputs first.
"get the value attribute from the element"
^ input attributeAt: 'value'.