我正在嘗試提取隱藏輸入標簽的值。即使該元素存在于 HTML 中,我也無法使用 bs4 找到它。
這是我收到的錯誤訊息:
AttributeError: 'NoneType' object has no attribute 'find'
這是網頁上的html:
<form id="exampleid" class="exampleclass" action="/ex/ex-ex/ex/2" method="post">
<more html>
<div>
<input type="hidden" name="csrf" value="abcdefghijklmnopqrstuvwxyz">
</div></form>
這是我當前的代碼:
csrf = soup.find("form", {"id": "exampleid"})
csrf = csrf.find('input', {'name': 'csrf'}).get("value")
print(csrf)
我會很感激任何形式的幫助,因為它真的困擾著我。先感謝您!
uj5u.com熱心網友回復:
您的選擇仍然有效,認為還有另一個問題,也許您不會得到您期望的 html。
作為選擇和獲取此隱藏值的替代方法,<input>您可以使用以下內容css selector:
soup.select_one('#exampleid input[name*="csrf"]')['value']
例子
from bs4 import BeautifulSoup
html = '''
<form id="exampleid" action="/ex/ex-ex/ex/2" method="post">
<div>
<input type="hidden" name="csrf" value="abcdefghijklmnopqrstuvwxyz">
</div></form>'''
soup = BeautifulSoup(html, "lxml")
csrf = soup.select_one('#exampleid input[name*="csrf"]')['value']
print(csrf)
輸出
abcdefghijklmnopqrstuvwxyz
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364917.html
上一篇:生成多個表單輸入php
