2017-11-13 7 views
0

내 ZnClient 요청에서 추출 및 저장하려는 키와 값을 포함하는 HTML 형식의 서버로부터 응답을받습니다. 나중에 사용하기 위해. 파로에서 어떻게합니까?pharo/smalltalk에서 서버 응답에 의해 반환 된 키/값을 추출하고 저장하는 방법

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

+0

당신은 가지고있는 HTML의 예제를 줄 수 있습니까? 카탈로그에서 XMLParser 프로젝트를로드 할 수 있다고 가정하기 때문에 HTML을 구문 분석하고 DOM 모델에서 dict (또는 연관 컬렉션)을 만듭니다. – Uko

+0

친애하는 @Uko 질문에 위의 서버 응답을 넣습니다. – ludo

+0

시도해 볼 수있는 PetitParser의 PPXmlParser도 있습니다. 그러나 인코딩 (대개 UTF-8)에도주의해야한다는 것을 나타내는 비 ASCII 문자를 붙여 넣었습니다. –

답변

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 
+0

친애하는 @draegtun 스프레드를 사용하기 위해 어떻게 HTML을 문자열로 변환 할 수 있습니까? – ludo

+0

@ludo -'ZnClient new get : 'http://www.example.com'asString.' – draegtun

+0

잘 작동 해 주셔서 감사합니다. 문제는 현재 하나의 csrfmiddlewaretoken으로 두 개의 출력 결과가 있다는 것입니다. 그 값만 추출하는 방법 – ludo

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