开发者

Using beautifulSoup, trying to get all table rows that have a string in them

开发者 https://www.devze.com 2023-01-10 14:05 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消