我想下载一些 html 页面并提取信息,每个 HTML 页面都有这个table tag
:
<table class="sobi2Details" style='background-image: url(http://www.imd.ir/components/com_sobi2/images/backgrounds/grey.gif);border-style: solid; border-color: #808080' >
<tr>
<td><h1>Dr Jhon Doe</h1></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div id="sobi2outer">
<br/>
<span id="sobi2Details_field_name" ><span id="sobi2Listing_field_name_label">name:</span>Jhon</span><br/>
<span id="sobi2Details_field_family" ><span id="sobi2Listing_field_family_label">family:</span> Doe</span><br/>
<span id="sobi2Details_field_tel1" ><span id="sobi2Listing_field_tel1_label">tel:</span> 33727464</span><br/>
</div>
</td>
</tr>
</table>
我想访问 name ( Jhone
) 、family ( Doe
) 和 tel( 33727464
),我使用beausiful soup通过 id 访问这些 span 标签:
name=soup.find(id="sobi2Details_field_name").__str__()
family=soup.find(id="sobi2Details_field_family").__str__()
tel=soup.find(id="sobi2Details_field_tel1").__str__()
但我不知道如何将数据提取到这些标签中。我尝试使用children
和content
属性,但是当我使用主题作为tag
它返回时None
:
name=soup.find(id="sobi2Details_field_name")
for child in name.children:
#process content inside
但我收到此错误:
'NoneType' object has no attribute 'children'
而当我在它上面使用str () 时,它不是None
!任何想法?
编辑:我的最终解决方案
soup = BeautifulSoup(page,from_encoding="utf-8")
name_span=soup.find(id="sobi2Details_field_name").__str__()
name=name_span.split(':')[-1]
result = re.sub('</span>', '',name)