簡體   English   中英

Python Beautifulsoup:如何在html標記的文本中擺脫“ \\ n”

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

我嘗試使用python將數據寫入csv文件:

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

但是我得到的是:

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

文本中有一些令人討厭的'\\ n',我該如何擺脫它們,使輸出更加優雅?

使用get_text(strip=True)

from bs4 import BeautifulSoup

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM