I need to get all table rows on a page that contain a specific string 'abc123123' in them.
The string is inside a TD, but I need the entire TR if it contains the 'abc123123' anywhere inside.
I tried this:
userrows = s.findAll('tr', contents = re.compile('abc123123'))
I'm not sure if contents is the write property.
My html lo开发者_如何学Coks something like:
<tr>
<td>
</td>
<td><table>.... abc123123 </table><tr>
..
</tr>
<tr>
..
</tr>
..
..
No, the extra keyword arguments beyond the specified ones (name, attrs, recursive, text, limit
) all refer to attributes of the tag you're searching for.
You cannot search for name
and text
at the same time (if you specify text
, BS ignores name
) so you need separate calls, e.g:
allrows = s.findAll('tr')
userrows = [t for t in allrows if t.findAll(text=re.compile('abc123123'))]
Here I'm using a list comprehension since I assume you want a list of the relevant tag objects, as findAll
itself gives you.
精彩评论