我正在做一些网页抓取,网站经常使用HTML实体来表示非ascii字符.Python是否有一个实用程序,它接受带有HTML实体的字符串并返回unicode类型?
例如:
我回来了:
ǎ
Run Code Online (Sandbox Code Playgroud)
代表带有音标的"ǎ".在二进制中,这表示为16位01ce.我想将html实体转换为值 u'\u01ce'
我已经四处寻找并且只找到了python 2.6及更早版本的解决方案,没有关于如何在python 3.X中执行此操作.(我只能访问Win7盒子.)
我必须能够在3.1中执行此操作,并且最好不使用外部库.目前,我安装了httplib2并访问命令提示符curl(这就是我获取页面源代码的方式).不幸的是,curl不解码html实体,据我所知,我找不到在文档中解码它的命令.
是的,我试图让美丽的汤工作,很多时候没有成功3.X. 如果您可以在MS Windows环境中提供有关如何在python 3中使用它的EXPLICIT说明,我将非常感激.
所以,要清楚,我需要将这样Suzy & John的字符串转换成这样的字符串:"Suzy&John".
我有一个像这样的符号的字符串:
'
Run Code Online (Sandbox Code Playgroud)
那显然是撇号.
我试了saxutils.unescape()没有运气,试过urllib.unquote()
我怎么解码这个?谢谢!
我有html编码的文本,如下所示:
RT <a href="http://twitter.com/freuter">@freuter</a>...
Run Code Online (Sandbox Code Playgroud)
我希望这显示为HTML,但我不确定是否有一个过滤器,我可以应用于此文本将html编码的文本转换回html ...
有人可以帮忙吗?
我正在尝试使用 django 使用feedgenerator.Rss201rev2Feed创建播客 rss 提要 作为提要生成器,它的工作方式与 BeautifulSoup 有点相反:将信息放入适当的 xml 标签中
它运行良好,但我不想转义所有html
特别是,我希望<itunes:summary>rss feed 的值显示如下:
<itunes:summary><![CDATA[Link to <a href="http://www.website.com">the website</a>]]></itunes:summary>
按照Apple 规范
如果我在普通视图中渲染 html,我可以在 html 模板中使用|safe过滤器。我现在需要类似的东西,以有选择地防止<在 rss feed 中转义。
也就是说,我需要 rss 出现<![CDATA[...]]而不是转义<![CDATA[...]]>
然而,Django似乎“无论如何,Django都会自动转义RSS提要(或任何与此相关的XML)中的特殊字符,无论您是否通过安全过滤器传递它”(请参阅this 2009 Question)
到目前为止还没有运气:
因此,迄今为止使用mark_safe 的尝试已被证明是无用的。
我也不确定如何解释一种想法,将“autoescape=False 传递给 django.contrib.synmination.feeds 中的 render() 调用”。
添加到 addQuickElement 注释的建议, escape=False返回了错误
handler.addQuickElement(u'itunes:summary',item['summary'], escape=False)
TypeError: addQuickElement() got an unexpected keyword argument 'escape' …Run Code Online (Sandbox Code Playgroud) 我有以下文字:
"It's the show your only friend and pastor have been talking about!
<i>Wonder Showzen</i> is a hilarious glimpse into the black
heart of childhood innocence! Get ready as the complete first season of MTV2's<i> Wonder Showzen</i> tackles valuable life lessons like birth,
nature, diversity, and history – all inside the prison of
your mind! Where else can you..."
Run Code Online (Sandbox Code Playgroud)
我想要做的是删除html标签并将其编码为unicode.我目前在做:
def remove_tags(text):
return TAG_RE.sub('', text)
Run Code Online (Sandbox Code Playgroud)
这只剥离了标签.我如何正确编码上面的数据库存储?
我有一个<img src=__string__>但字符串可能包含",我该怎么做才能逃脱它?
例:
__string__ = test".jpg
<img src="test".jpg">
Run Code Online (Sandbox Code Playgroud)
不起作用.
如何解码这样的unicode字符串:
什么%2527s%2bthe%2btime%252C%2bnow%253F
像这样的ascii:
什么+的+时间+现在