Using BeautifulSoup to find all items starting with a given letter

If I want to find all <p> elementwith id = test with BeautifulSoup, I use:

for item in soup.findAll('p', {"id": "test"}): 

How to find each item

with an identifier starting with a certain letter - let them say "t"?

I tried "t *" but it does not work.

+4
source share
3 answers

to try:

 import re for item in soup.findAll('p', {"id": re.compile('^t')}): 
+8
source
 for item in soup.findAll('p', {"id": lambda x: x and x.startswith('t')}): 
+2
source

Try the following:

 for item in soup.find_all('p', id=re.compile('^test')): 
0
source

Source: https://habr.com/ru/post/1341062/


All Articles