簡體   English   中英

Python unicode字符串到字符串?

[英]Python unicode string to string?

這將獲得帶有class =“ title”的最后一個div的內容:

soup = BeautifulSoup(html)
title = soup.find_all('div', {'class' : 'title' })[-1].contents

當我print(title)它輸出:

[u'Harry Potter']

我如何才能僅打印它:

Harry Potter

我嘗試了print(str(title))但是沒有任何改變。

如果您希望轉換unicode而不是像@alecxe那樣獲取字符串對象,則必須對其進行編碼。

print(title.encode('utf-8'))應該可以工作。 如果仍然在方括號中,只需執行print(title[0].encode('utf-8'))

只需使用get_text() 獲取文本 get_text()

title = soup.find_all('div', {'class' : 'title' })[-1].get_text()

暫無
暫無

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

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