while using beautifulsoup to parse a table in html every other row starts with
<tr class="row_k">
instead of a tr tag without a class
Sample HTML
<tr class="row_k">
<td><img src="some picture url" alt="Item A"></td>
<td><a href="some url"> Item A</a></td>
<td>14.8k</td>
<td><span class="drop">-555</span></td>
<td>
<img src="some picture url" alt="stuff" title="stuff">
</td>
<td>
<img src="some picture url" alt="Max llll">
</td>
</tr>
<tr>
<td><img src="some picture url" alt="Item B"></td>
<td><a href="some url"> Item B</a></td>
<td>64.9k</td>
<td><span class="rise">+165</span></td>
<td>
<img src="some picture url" alt="stuff" title="stuff">
</td>
<td>
<img src="some picture url" alt="max llll">
</td>
</tr>
<tr class="row_k">
<td><img src="some picture url" alt="Item C"></td>
<td><a href="some url"> Item C</a></td>
<td>4,000</td>
<td><span class="rise">+666</span></td>
<td>
<img src="some picture url" title="stuff">
</td>
<td>
<img src="some picture url" alt="Maximum lllle">
Text I wish to extract is 14.8k, 64.9k, and 4,000
this1 = urllib2.urlopen('my url').read()
this_1 = BeautifulSoup(this1)
this_1a = StringIO.StringIO()
for row in this_1.findAll("tr", { "class" : "row_k" }):
for col in row.findAll(re.compile('td')):
this_1a.write(col.string if col.string else '')
Item_this1 = this_1a.getvalue()
I get the feeling that this code is poorly written, Is there a more flexible tool I can use such as an XML parser? that someone could suggest.
still open to any answers that still utilize beautifulsoup.