-4

I have the following html code from where I extract the text "Classification of protein families" with BS in python.

<h1 class="item-title__primary">

    
        Classification of protein families
    
However, when I export the data to a excel file, the text comes with a lot of spaces. How can I overcome this issue? Thank you.

Web scraping code:

titles.append(soup.find('h1',class_='item-title__primary').text)

1 Answers1

0
e = "  word  "

print(e.strip())

# "word"

e = "word  and  word 2"

print(e.replace("  ", " "))

# word and word 2
eyal
  • 107
  • 1
  • 7
  • If the expected result is a string where all the adjacent whitespaces are converted into one space, neither of the suggested solutions will work. I'd do `' '.join(s.split())` to do this – Kolay.Ne Mar 28 '21 at 17:06