how to remove text between [removed] and [removed] using python?

后端 未结 9 644
眼角桃花
眼角桃花 2021-02-04 19:19

how to remove text between using python?

相关标签:
9条回答
  • 2021-02-04 20:16

    Element Tree is the best simplest and sweetest package to do this. Yes, there are other ways to do it too; but don't use any 'coz they suck! (via Mark Pilgrim)

    0 讨论(0)
  • 2021-02-04 20:18

    If you don't want to import any modules:

    string = "<script> this is some js. begone! </script>"
    
    string = string.split(' ')
    
    for i, s in enumerate(string):
        if s == '<script>' or s == '</script>' :
            del string[i]
    
    print ' '.join(string)
    
    0 讨论(0)
  • 2021-02-04 20:21

    You can use BeautifulSoup with this (and other) methods:

    soup = BeautifulSoup(source.lower())
    to_extract = soup.findAll('script')
    for item in to_extract:
        item.extract()
    

    This actually removes the nodes from the HTML. If you wanted to leave the empty <script></script> tags you'll have to work with the item attributes rather than just extracting it from the soup.

    0 讨论(0)
提交回复
热议问题