Removing new line '\n' from the output of python BeautifulSoup

前端 未结 3 1890
难免孤独
难免孤独 2021-01-12 01:52

I am using python Beautiful soup to get the contents of:

abc def
3条回答
  •  抹茶落季
    2021-01-12 02:17

    You could do this:

    breadcrum = [item.strip() for item in breadcrum if str(item)]
    

    The if str(item) will take care of getting rid of the empty list items after stripping the new line characters.

    If you want to join the strings, then do:

    ','.join(breadcrum)
    

    This will give you abc,def,ghi

    EDIT

    Although the above gives you what you want, as pointed out by others in the thread, the way you are using BS to extract anchor texts is not correct. Once you have the div of your interest, you should be using it to get it's children and then get the anchor text. As:

    path = soup.find('div',attrs={'class':'path'})
    anchors = path.find_all('a')
    data = []
    for ele in anchors:
        data.append(ele.text)
    

    And then do a ','.join(data)

提交回复
热议问题