从HTML页面处理python中的变量特殊字符
当我使用Mechanize获取html数据时,我将其存储到一个变量中,我们称之为“html\U响应”。完成后,我将对其进行解析并提取三件事: 标题、简短描述和长篇描述 我面临的问题是,短描述或长描述可能包含-&、£、$等字符 当我尝试将其放入XML并保存时,问题就出现了,因为当我尝试解码这些内容时,python会崩溃 例如,以下是页面中的简短说明:从HTML页面处理python中的变量特殊字符,python,encoding,utf-8,ascii,Python,Encoding,Utf 8,Ascii,当我使用Mechanize获取html数据时,我将其存储到一个变量中,我们称之为“html\U响应”。完成后,我将对其进行解析并提取三件事: 标题、简短描述和长篇描述 我面临的问题是,短描述或长描述可能包含-&、£、$等字符 当我尝试将其放入XML并保存时,问题就出现了,因为当我尝试解码这些内容时,python会崩溃 例如,以下是页面中的简短说明: S_DESC = "Senior VP of Treasury and Corporate Finance & ERM, RTL Grou
S_DESC = "Senior VP of Treasury and Corporate Finance & ERM,
RTL Group, has been invited to the above conference to present a Case Study
on Integrating Strategy and Risk into Enterprise Risk Management"
我解码的方式-
#!/usr/bin/python
# -*- coding: ISO-8859-1 -*-
print S_DESC.decode('UTF-8').encode('ascii','xmlcharrefreplace')
这在安培上很好用。
如果我随后得到一个带有英镑符号的S_DESC,我的脚本将以以下输出中断:
UnicodeEncodeError:“ascii”编解码器无法对字符u'\xa3'进行编码。
这段代码在我的脚本部分失败(上面的异常在最后一行抛出,每次我得到一个英镑符号)。我想知道是否有一种通用的方法告诉python自己处理这些字符。
为每个可能不兼容的字符创建100个函数不是一个选项,同样地,我也不准备筛选整个网站(2k+文章),以便识别所有“可能”丢弃我代码的特殊字符
XML = """
<MAIN>
<ITEM>
<Author>{0}</Author>
<Author_UN>{1}</Author_UN>
<Date_Modified>{2}</Date_Modified>
<Date_Published>{3}</Date_Published>
<Default_Group_Rights>
{4}
</Default_Group_Rights>
<attachment>
<file_name>{5}</file_name>
<file_extension>{6}</file_extension>
<file_stored_local>{7}</file_stored_local>
</attachment>
<title>{8}</title>
<sm_desc>{9}</sm_desc>
<lg_desc>
<![CDATA[
{10}
]]>
</lg_desc>
</ITEM>
</MAIN>""".format(author_soup, username, date_modified, published_date, xrights, attachment_text, file_extension, localstore, item_title.decode('UTF-8').encode('ascii','xmlcharrefreplace'), short_description.decode('UTF-8').encode('ascii','xmlcharrefreplace'), long_description.decode('UTF-8').encode('ascii','xmlcharrefreplace'))
XML=”“”
]'
XML=”“”
{0}
{1}
{2}
{3}
{4}
{5}
{6}
{7}
{8}
{9}
“.”格式(作者、用户名、修改日期、发布日期、版权、附件文本、文件扩展名、本地存储、项目标题、解码('UTF-8')、短描述、解码('UTF-8')、长描述、解码('UTF-8'))
为什么要使用ASCII?XML支持Unicode很好。您只需要编码“
,
,以及&
。请详细说明如何实现这一点?我一点也不受ascii的束缚,我只是想让它工作。我将如何编码这些字符以及在哪里?您能否提供一个示例代码,使用我在前面的评论中使用的这句话作为变量:“$1000000用于改革和变革管理”,因为您是从UTF-8(一种unicode编码)解码的,它已经是unicode的。你为什么要这么做?仅供参考,这里有一个很好的unicode介绍。只有读了这篇文章,我才真正理解它!非常感谢。我一定要看一看。我需要明白这一点,因为这让我非常生气。。。
#TESTING GROUND
# -*- coding: UTF-8 -*-
author_soup = "John Smith"
username = "jsmith"
date_modified = "25 December 2012, 15:42 PM"
published_date = "25 December 2012, 15:42 PM"
xrights = "r-w-x-x"
attachment_text = "Random Attachment"
file_extension = "txt"
localstore = "../Local"
item_title = "The NEw Financial Reforms of 2012"
short_description = " £16 Billion Spent on new reforms backfire."
long_description = '[<p>fullstory</p>, <p><a class="external-link" href="http://business.timesonline.co.uk/tol/business/industry_sectors/banking_and_finance/article4526065.ece">http://business.timesonline.co.uk/tol/business/industry_sectors/banking_and_finance/article4526065.ece</a></p>]'
XML = """
<MAIN>
<ITEM>
<Author>{0}</Author>
<Author_UN>{1}</Author_UN>
<Date_Modified>{2}</Date_Modified>
<Date_Published>{3}</Date_Published>
<Default_Group_Rights>
{4}
</Default_Group_Rights>
<attachment>
<file_name>{5}</file_name>
<file_extension>{6}</file_extension>
<file_stored_local>{7}</file_stored_local>
</attachment>
<title>{8}</title>
<sm_desc>{9}</sm_desc>
<lg_desc>
<![CDATA[
{10}
]]>
</lg_desc>
</ITEM>
</MAIN>""".format(author_soup, username, date_modified, published_date, xrights, attachment_text, file_extension, localstore, item_title.decode('UTF-8'), short_description.decode('UTF-8'), long_description.decode('UTF-8'))