I want to download some html pages and extract informations, each HTML page has this 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>
I want to access name (Jhone) ,family (Doe) and tel(33727464),I've used beausiful soup to access these span tags by id:
name=soup.find(id="sobi2Details_field_name").__str__()
family=soup.find(id="sobi2Details_field_family").__str__()
tel=soup.find(id="sobi2Details_field_tel1").__str__()
but I don't know how to extract data into these tags.I tryed to use children and content attributes,but when I use theme as a tag It returns None:
name=soup.find(id="sobi2Details_field_name")
for child in name.children:
#process content inside
but I get this error:
'NoneType' object has no attribute 'children'
while when I use str() on it,it is not None!!
any Idea?
Edit:My final solution
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)
type(name)return -- for me it returned <class 'bs4.element.Tag'>. I just installed BS4 with easy_install on Python 2.7.2 on OS X 10.8.print type(name)after thename=soup.find(...)line, you will be able to tell what type BS has returned for the result of thefindmethod.