gpt4 book ai didi

python - 使用 beautifulsoup 从表单中提取隐藏值

转载 作者:行者123 更新时间:2023-12-04 01:08:18 25 4
gpt4 key购买 nike

我正在尝试解析以下页面 HTML 响应(没有正确的 HTML 页面格式 head/body...等):

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

然而,这并没有奏效。

提取两个隐藏“值”的更优雅的方法是什么?

<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

关于python - 使用 beautifulsoup 从表单中提取隐藏值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65619802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com