用美汤从表单中提取隐藏值

2022-02-21 00:00:00 python beautifulsoup

问题描述

我正在尝试解析页面HTML响应,该响应如下(没有正确的HTML页面格式、头/正文.等):

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><img src=" /sites/all/themes/iprn_bootstrap/images/ico_close.png"></button>
        <h4 class="modal-title">Login</h4>
    </div>
    <div id="ajax-forms-messages"></div>
    <div class="modal-body"><form action="/user/signin?destination=user/signin" method="post" id="user-login-form" accept-charset="UTF-8"><div><div class="form-item form-item-name form-type-textfield form-group"><input placeholder="E-mail *" class="form-control form-text required" type="text" id="edit-name" name="name" value="" size="15" maxlength="60" /> <label class="control-label element-invisible" for="edit-name">E-mail <span class="form-required" title="This field is required.">*</span></label>
</div><div class="form-item form-item-pass form-type-password form-group"><input placeholder="Password *" class="form-control form-text required" type="password" id="edit-pass" name="pass" size="15" maxlength="128" /> <label class="control-label element-invisible" for="edit-pass">Password <span class="form-required" title="This field is required.">*</span></label>
</div><input type="hidden" name="form_build_id" value="form-lLpMWePGycFNEi-XgTGuRW-vy2lNBld-rtTYEKX7JHk" />
<input type="hidden" name="form_id" value="user_login_block" />
<div class="form-actions form-wrapper form-group" id="edit-actions"><button id="login-submit-btn" type="submit" name="op" value="Log in" class="btn btn-primary form-submit icon-before"><span class="icon glyphicon glyphicon-log-in" aria-hidden="true"></span>
 Log in</button>
</div><a href="/user/password" class="forget" title="Request new password">Forgot your password?</a>
    <div class="section white">Not registered? <a href="/user/registration" class="link-register">Register</a></div></div></form></div>

我正在尝试获取";隐藏";输入的值,但在处理BeautifulSoup时遇到了困难,因为我正在尝试:

soup.select_one('#form_build_id')['value']

但是,这没有起作用。

哪种更优雅的方法可以同时提取隐藏的&Quot;值&Quot;?

<input type="hidden" name="form_build_id" value="form-lLpMWePGycFNEi-XgTGuRW-vy2lNBld-rtTYEKX7JHk" />
<input type="hidden" name="form_id" value="user_login_block" />

解决方案

只需搜索type="hidden"

...
soup = BeautifulSoup(html, "html.parser")

for tag in soup.find_all("input", type="hidden"):
    print(tag["value"])

输出:

form-lLpMWePGycFNEi-XgTGuRW-vy2lNBld-rtTYEKX7JHk
user_login_block

相关文章