Matching partial ids in BeautifulSoup

前端 未结 4 514
一整个雨季
一整个雨季 2020-11-29 07:21

I\'m using BeautifulSoup. I have to find any reference to the

tags with id like: post-#.

For example:

4条回答
  •  迷失自我
    2020-11-29 08:03

    You can pass a function to findAll:

    >>> print soupHandler.findAll('div', id=lambda x: x and x.startswith('post-'))
    [
    ...
    ,
    ...
    ]

    Or a regular expression:

    >>> print soupHandler.findAll('div', id=re.compile('^post-'))
    [
    ...
    ,
    ...
    ]

提交回复
热议问题