[英]Python - BeautifulSoup extract value from multiple options
Code: 码:
from bs4 import BeautifulSoup
import requests
html = ''' <div class="fieldset-content checkout-login__footer">
<hr class="checkout-login__newsletter-rule">
<div class="row checkout-login__footer with-info">
<div class="columns medium-9 checkout-login__subscribtion-container">
<input type="checkbox" name="subscriptionForms[0]_value" id="emailSignUp">
<label for="emailSignUp">
I have read the <a href="/en-gb/customer-care/privacy-policy">Privacy Policy</a> and give consent to email me with updates about Clos19, including the latest product releases and special events.<small class="info js-privacy-policy-info hide"> Uncheck this box to opt out</small>
</label>
<input type="hidden" class="hidden" name="subscriptionForms[0]_type" value="NEWSLETTER" />
<input type="hidden" class="hidden" name="subscriptionForms[0]_location" value="CHECKOUT" />
</div>
<div class="columns medium-3 small-12">
<button type="submit" class="button button-primary float-right positive">Continue</button>
</div>
</div>
</div>
<div>
<input type="hidden" name="CSRFToken" value="80f1330a-7a4e-4878-a6ab-710356f47961" /> '''
soup = BeautifulSoup(html, 'html.parser')
CSRFToken = soup.find('input', type='hidden')["value"]
print(CSRFToken)
I am trying to print 80f1330a-7a4e-4878-a6ab-710356f47961 but the terminal is returning NEWSLETTER as it is the first it finds. 我正在尝试打印80f1330a-7a4e-4878-a6ab-710356f47961,但是终端返回的是NEWSLETTER,因为这是它找到的第一个。
How do I find the value of value when name="CSRFToken"? 当name =“ CSRFToken”时,如何找到value的值?
You can use the name
parameter in the find
call: 您可以在
find
调用中使用name
参数:
from bs4 import BeautifulSoup as soup
print(soup(html, 'html.parser').find('input', {'name':"CSRFToken"})['value'])
Output: 输出:
80f1330a-7a4e-4878-a6ab-710356f47961
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.