KengoTokukawa
KengoTokukawa

Reputation: 55

Python Beautifulsoup:how to get rid of '\n' in the texts of html tag

I've tried to write data to a csv file using python:

soup = BeautifulSoup(html)
ticketCount=soup.find_all('span',id='ticket_count')
records.append(ticketCount)

But what I got is:

<span id=""ticket_count"">15695.3\n                   \u4e07\n            </span>]

there are some annoying '\n' in the text, how can I get rid of them and make my output more elegant?

Upvotes: 1

Views: 1068

Answers (1)

alecxe
alecxe

Reputation: 473863

Use get_text(strip=True):

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
ticketCount = soup.find('span', id='ticket_count').get_text(strip=True)
print(ticketCount)

Upvotes: 2

Related Questions