如何在pharo / smalltalk中提取和存储由服务器响应返回的键/值

时间:2017-11-13 10:37:36

标签: pharo

在我的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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以使用Soup等HTML解析器提取此信息。

这是一个简短的工作示例:

|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 

答案 1 :(得分:0)

您可以从Pharo目录中加载XMLParserHTMLXPath。那么这应该可以解决问题:

| 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'.