【问题标题】:lxml clean breaks href attributelxml 彻底打破 href 属性
【发布时间】:2015-06-06 16:26:43
【问题描述】:
import lxml.html.clean as clean
cleaner = clean.Cleaner(style=True, remove_tags=['div','span',], safe_attrs_only=['href',])
text = cleaner.clean_html('<a href="http://жк-сочи.рф/">link</a>')
print text

打印

<a href="http://%C3%90%C2%B6%C3%90%C2%BA-%C3%91%C2%81%C3%90%C2%BE%C3%91%C2%87%C3%90%C2%B8.%C3%91%C2%80%C3%91%C2%84/">link</a>

如何获得:

<a href="http://жк-сочи.рф/">link</a>

即普通编码中的href?

【问题讨论】:

  • 试试 lxml.html.tostring(text , encoding="ascii")

标签: lxml


【解决方案1】:

clean 做对了——括号中的字符串应该被正确编码,看似乱码的东西就是正确的编码。

您可能不知道,但不存在 kyrillic 域名 - 有一个复杂的系统可以将这些域名映射到“允许的”字符。

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 2021-11-03
    • 2015-02-21
    • 2010-12-30
    • 2015-06-23
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多