【发布时间】:2019-06-19 08:13:21
【问题描述】:
我仍然是一个尝试学习beautifulsoup 的python 菜鸟。我查看了堆栈上的解决方案但没有成功请帮助我更好地理解这一点。 我已经提取了如下所示的html
<table cellspacing="0" id="ContentPlaceHolder1_dlDetails"
style="width:100%;border-collapse:collapse;">
<tbody><tr>
<td>
<table border="0" cellpadding="5" cellspacing="0" width="70%">
<tbody><tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td bgcolor="#4F95FF" class="listhead" width="49%">Location:</td>
<td bgcolor="#4F95FF" class="listhead" width="51%">On Site </td>
</tr>
<tr>
<td class="listmaintext">ATM ID: </td>
<td class="listmaintext">DAGR00401111111</td>
</tr>
<tr>
<td class="listmaintext">ATM Centre:</td>
<td class="listmaintext"></td>
</tr>
<tr>
<td class="listmaintext">Site Location: </td>
<td class="listmaintext">ADA Building - Agra</td>
</tr>
我试图解析 find_all('tbody') 但不成功
#table = bs.find("table", {"id": "ContentPlaceHolder1_dlDetails"})
html = browser.page_source
soup = bs(html, "lxml")
table = soup.find_all('table', {'id':'ContentPlaceHolder1_dlDetails'})
table_body = table.find('tbody')
rows = table.select('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])values
我正在尝试将值保存在“listmaintext”类中
错误信息
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
【问题讨论】:
-
如果您指定了所需的输出格式,这将有所帮助。上面你似乎想要一个列表。
-
所需的输出格式为 ATM ID:DAGR00401111111 ATM 中心:站点位置:ADA 大楼 - Agra Link 分支:Sol ID:54000 州:北方邦区:阿格拉 场外地址:场内地址:联合印度银行,Adra Development Authority- Agra Branch, Agra Development Authority, Agra, Jaipur House, Loha Mandi, Uttar Pradesh - 282010 密码:在 csv 或 json 中,因为我有多个具有相同标题的 html
标签: python python-3.x html-table beautifulsoup