Using BeautifulSoup to find all elements starting with a given letter
If I want to find all <p> elementswith id=test with BeautifulSoup, I use :
for item in soup.findAll('p', {"id": "test"}):
How do I find every
element with an ID开发者_StackOverflow中文版 starting with a specific letter - let's say "t"?
I tried "t*" but it doesn't work.
try:
import re
for item in soup.findAll('p', {"id": re.compile('^t')}):
for item in soup.findAll('p', {"id": lambda x: x and x.startswith('t')}):
Try this:
for item in soup.find_all('p', id=re.compile('^test')):
精彩评论