David wrote: > <td> </td> > > <td width="1%" class=key>Open: > </td> > <td width="1%" class=val>5.50 > </td> > <td> </td> > <td width="1%" class=key>Mkt Cap: > </td> > <td width="1%" class=val>6.92M > </td> > <td> </td> > <td width="1%" class=key>P/E: > </td> > <td width="1%" class=val>21.99 > </td> > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas?
>>> from BeautifulSoup import BeautifulSoup >>> bs = BeautifulSoup("""<td> </td> ... ... <td width="1%" class=key>Open: ... </td> ... <td width="1%" class=val>5.50 ... </td> ... <td> </td> ... <td width="1%" class=key>Mkt Cap: ... </td> ... <td width="1%" class=val>6.92M ... </td> ... <td> </td> ... <td width="1%" class=key>P/E: ... </td> ... <td width="1%" class=val>21.99 ... </td> ... """) >>> for key in bs.findAll(attrs={"class": "key"}): ... value = key.findNext(attrs={"class": "val"}) ... print key.string.strip(), "-->", value.string.strip() ... Open: --> 5.50 Mkt Cap: --> 6.92M P/E: --> 21.99 -- http://mail.python.org/mailman/listinfo/python-list