具有相同类的div的Beautifulsoup打印属性值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了具有相同类的div的Beautifulsoup打印属性值相关的知识,希望对你有一定的参考价值。
我有以下代码工作,将在value =之后打印文本
soup = BeautifulSoup(html, 'lxml')
name = soup.find('input')['value']
print(name)
然而,该页面有多个div与我尝试过的同一个类相同但我得到错误并且只能打印第一个字段值,即Name。
请参阅随附的屏幕截图
<div class="control-group"><label class="control-label required" for="client_appbundle_prospecttype_ProspectFirstContact_decision_timeframe">What date do you want to make a decision?</label>
<div class="controls"><input type="text" id="client_appbundle_prospecttype_ProspectFirstContact_decision_timeframe" name="client_appbundle_prospecttype[ProspectFirstContact][decision_timeframe]" required="required" class="input-small text-bound datepicker hasDatepicker"></div>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane active" id="prospect_consultation">
<div class="widget row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<h6>Personal details</h6>
</div>
</div>
<div class="well">
<div class="control-group">
<label class="control-label">Name</label>
<div class="controls">
Sam Test-March 2018
</div>
</div>
<div class="control-group">
<label class="control-label">Address and postcode</label>
<div class="controls">
</div>
</div>
<div class="control-group">
<label class="control-label">Mobile number</label>
<div class="controls">
12345678
</div>
</div>
<div class="control-group">
<label class="control-label">Email address</label>
<div class="controls">
test@test.com
</div>
</div>
谢谢!
答案
也许是这样的:
from bs4 import BeautifulSoup
html = '''
<html>
<head></head>
<body>
<div class="control-group">
<label class="control-label required" for="client_appbundle_prospecttype_ProspectFirstContact_decision_timeframe">What date do you want to make a decision?</label>
<div class="controls">
<input type="text" id="client_appbundle_prospecttype_ProspectFirstContact_decision_timeframe" name="client_appbundle_prospecttype[ProspectFirstContact][decision_timeframe]" required class="input-small text-bound datepicker hasDatepicker">
</div>
</div>
<div class="tab-pane active" id="prospect_consultation">
<div class="widget row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<h6>Personal details</h6>
</div>
</div>
<div class="well">
<div class="control-group">
<label class="control-label">Name</label>
<div class="controls">
Sam Test-March 2018
</div>
</div>
<div class="control-group">
<label class="control-label">Address and postcode</label>
<div class="controls">
</div>
</div>
<div class="control-group">
<label class="control-label">Mobile number</label>
<div class="controls">
12345678
</div>
</div>
<div class="control-group">
<label class="control-label">Email address</label>
<div class="controls">
test@test.com
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
items = soup.select('.controls')
print([item.text.strip() for item in items if item.text.strip()])
以上是关于具有相同类的div的Beautifulsoup打印属性值的主要内容,如果未能解决你的问题,请参考以下文章
在具有相同类的一组 div 中悬停在单个 div 上时显示覆盖