Strip HTML from strings in Python

前端 未结 26 2339
难免孤独
难免孤独 2020-11-22 02:50
from mechanize import Browser
br = Browser()
br.open(\'http://somewebpage\')
html = br.response().readlines()
for line in html:
  print line

When p

相关标签:
26条回答
  • 2020-11-22 03:21

    I haven't thought much about the cases it will miss, but you can do a simple regex:

    re.sub('<[^<]+?>', '', text)
    

    For those that don't understand regex, this searches for a string <...>, where the inner content is made of one or more (+) characters that isn't a <. The ? means that it will match the smallest string it can find. For example given <p>Hello</p>, it will match <'p> and </p> separately with the ?. Without it, it will match the entire string <..Hello..>.

    If non-tag < appears in html (eg. 2 < 3), it should be written as an escape sequence &... anyway so the ^< may be unnecessary.

    0 讨论(0)
  • 2020-11-22 03:21

    You can use BeautifulSoup get_text() feature.

    from bs4 import BeautifulSoup
    
    html_str = '''
    <td><a href="http://www.fakewebsite.com">Please can you strip me?</a>
    <br/><a href="http://www.fakewebsite.com">I am waiting....</a>
    </td>
    '''
    soup = BeautifulSoup(html_str)
    
    print(soup.get_text()) 
    #or via attribute of Soup Object: print(soup.text)
    

    It is advisable to explicitly specify the parser, for example as BeautifulSoup(html_str, features="html.parser"), for the output to be reproducible.

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