Strip HTML tags to get strings in python

后端 未结 3 1076
太阳男子
太阳男子 2021-01-21 12:24

I tried to get some strings from an HTML file with BeautifulSoup and everytime I work with it I get partial results.

I want to get the strings in every li element/tag. S

3条回答
  •  甜味超标
    2021-01-21 13:14

    Iterate over results and get the value of text attribute:

    for element in soup.select(".sidebar li"):
        print element.text
    

    Example:

    from bs4 import BeautifulSoup
    
    
    data = """
    
        
    • Def Leppard - Make Love Like A ManLive
    • Inxs - Never Tear Us Apart
    """ soup = BeautifulSoup(data) for element in soup.select('li'): print element.text

    prints:

    Def Leppard -  Make Love Like A ManLive 
    Inxs - Never Tear Us Apart        
    

提交回复
热议问题