[英]How to convert unicode text to normal text
我正在学习Python中的美丽汤。
我试图解析一个包含书籍列表的简单网页。
例如
<a href="https://www.nostarch.com/carhacking">The Car Hacker’s Handbook</a>
我使用下面的代码。
import requests, bs4
res = requests.get('http://nostarch.com')
res.raise_for_status()
nSoup = bs4.BeautifulSoup(res.text,"html.parser")
elems = nSoup.select('.product-body a')
#elems[0] gives
<a href="https://www.nostarch.com/carhacking">The Car Hacker\u2019s Handbook</a>
和
#elems[0].getText() gives
u'The Car Hacker\u2019s Handbook'
但我想要的是正确的文字,
s = elems[0].getText()
print s
>>>The Car Hacker’s Handbook
如何修改我的代码以便给出“The Car Hacker's Handbook”输出而不是“u'The Car Hacker \\ u2019s Handbook”?
请帮助。
你尝试过使用编码方法吗?
elems[0].getText().encode('utf-8')
有关unicode和python的更多信息, 请访问https://docs.python.org/2/howto/unicode.html
此外,要发现您的字符串是否真的是utf-8编码,您可以使用chardet并运行以下命令:
>>> import chardet
>>> chardet.detect(elems[0].getText())
{'confidence': 0.5, 'encoding': 'utf-8'}
你可以试试
import unicodedata
def normText(unicodeText):
return unicodedata.normalize('NFKD', unicodeText).encode('ascii','ignore')
这会将unicodetext转换为纯文本,您可以写入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.