开发者

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')):
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜