Using BeautifulSoup to find a HTML tag that contains certain text

前端 未结 3 1504
情歌与酒
情歌与酒 2020-11-28 08:12

I\'m trying to get the elements in an HTML doc that contain the following pattern of text: #\\S{11}

this is cool #12345678901

<
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 09:11

    from BeautifulSoup import BeautifulSoup
    import re
    
    html_text = """
    

    this is cool #12345678901

    this is nothing

    foo #126666678901

    this is interesting #126666678901

    this is blah #124445678901

    """ soup = BeautifulSoup(html_text) for elem in soup(text=re.compile(r' #\S{11}')): print elem.parent

    Prints:

    this is cool #12345678901

    this is interesting #126666678901

    this is blah #124445678901

提交回复
热议问题