Strip HTML from strings in Python

前端 未结 26 2671
难免孤独
难免孤独 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

    You can use BeautifulSoup get_text() feature.

    from bs4 import BeautifulSoup
    
    html_str = '''
    Please can you strip me?
    
    I am waiting.... ''' 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.

提交回复
热议问题