अजगर में सक्षम नहीं करने के लिए मूल्य निकालने के इनपुट

0

सवाल

मैं कोशिश कर रहा हूँ निकालने के लिए मूल्य का एक छिपा इनपुट टैग में. भले ही तत्व मौजूद है 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)

मैं सराहना करते हैं, किसी भी तरह की मदद के रूप में यह वास्तव में मुझे परेशान. आप पहले से धन्यवाद!

beautifulsoup forms hidden-field python
2021-11-23 17:09:09
1

सबसे अच्छा जवाब

1

अपने चयन अभी भी काम कर रहा है, लगता है कि वहाँ एक और मुद्दा है, हो सकता है कि आप अभ्यस्त आप html की उम्मीद है.

के रूप में alternativ का चयन करने के लिए और मूल्य के इस छिपे हुए <input> आप उपयोग कर सकते हैं निम्नलिखित css selector:

soup.select_one('#exampleid input[name*="csrf"]')['value']

उदाहरण

from bs4 import BeautifulSoup

html = '''
<form id="exampleid" class="exampleclass" 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
2021-11-24 07:51:04

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में