Greg
Greg

Reputation: 597

BeautifulSoup find() not finding anything

Here is an example that replicates the problem I am having:

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup('<root><td><label for="pol_nbr">Policy number<br></label></td></root>', 'html.parser')
anchor = soup.find('label', text=re.compile('Policy number'))

But anchor is none. Why is it not the label tag?

Upvotes: 0

Views: 192

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

The issue is the <br> tag, if you remove it the code works:

In [39]: soup = BeautifulSoup('<root><td><label for="pol_nbr">Policy number</label></td></root>', 'html.parser')

In [40]: soup.find('label', text=re.compile('Policy number'))
Out[40]: <label for="pol_nbr">Policy number</label>

A more reliable way may be to select using the for attribute value:

soup.select_one("root label[for=pol_nbr]")

Upvotes: 1

Related Questions