python - Python 编码 '\xdf' / '\xc3\x9f' 到 'ß'
问题描述
我正在下载一个网站的 html 并用这样的漂亮汤美化它:
f_page_soup = Soup(f_driver.page_source, "lxml")
with open(f_filename_pretty, 'wb') as f_output:
f_output.write(f_page_soup.prettify(encoding='utf-8'))
并像这样打开美化的html:
ecjData = open(filename, 'r', encoding='utf-8').read()
pageSoup = Soup(ecjData, "lxml")
html 内部是我想用 BeautifulSoup 收集的不同链接。其中一个看起来像这样example.com/weiß/3
遍历所有链接后,我想打印它们。这样做:
print ("https://example.com" + a["href"])
UnicodeEncodeError
正如预期的那样,给了我上面的链接。
因此,在发现错误后,我尝试对其进行解码:
print (("https://example.com" + a["href"]).encode('utf-8').decode('latin-1'))
这导致
'ascii' codec can't encode characters in position 78-79: ordinal not in range(128)
我尝试的另一种方法是在字符串内部替换:
print (str(("https://example.com" + a["href"]).encode('utf-8')).replace('\\xc3\\x9f','ß'))
然后再次导致:
'ascii' codec can't encode character '\xdf' in position 80: ordinal not in range(128)
基本上我需要打印的是:
"https://example.com/weiß/3"
我怎样才能做到这一点?我正在使用python 3.5。
解决方案
推荐阅读
- node.js - 在带有“已启动”事件的 NodeJS 中使用 EventEmitter 时出现问题
- flutter - 如何在颤动中使用容器创建垂直固定的自定义标签栏
- swift - 如何快速删除字母uitextfield文本之间的多余空格
- regex - 如何从 UNIX 中管道分隔文件的第 n 列的值中删除所有非数字字符
- java - 从 /data/app 提取 apk 文件时出现 Apk 未安装错误
- spring-boot - 带有 spring-boot-starter-web 的 Spring Cloud Gateway
- sql - 如果当前月份不存在,是否有用于插入上个月记录的 SQL INSERT 语句?
- java - 在 AWS S3 上批量移动文件
- php - 如何删除数组中的匹配值
- wordpress - 如何在帖子循环内的静态 wordpress 首页上显示自定义帖子类型?