Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解码Python字符串中的HTML实体?_Python_Html_Html Entities - Fatal编程技术网

解码Python字符串中的HTML实体?

解码Python字符串中的HTML实体?,python,html,html-entities,Python,Html,Html Entities,我正在用Beauty Soup 3解析一些HTML,但它包含Beauty Soup 3不会自动解码的HTML实体: >>> from BeautifulSoup import BeautifulSoup >>> soup = BeautifulSoup("<p>&pound;682m</p>") >>> text = soup.find("p").string >>> print text

我正在用Beauty Soup 3解析一些HTML,但它包含Beauty Soup 3不会自动解码的HTML实体:

>>> from BeautifulSoup import BeautifulSoup

>>> soup = BeautifulSoup("<p>&pound;682m</p>")
>>> text = soup.find("p").string

>>> print text
&pound;682m
>>从BeautifulSoup导入BeautifulSoup
>>>汤=美丽的汤(6.82亿英镑)
>>>text=soup.find(“p”).string
>>>打印文本
&磅;682米
如何对
文本中的HTML实体进行解码,以获得
“682m英镑”
而不是
“682m英镑”

Python 3.4+ 使用:

仅供参考
html.parser.HTMLParser.unescape
已被弃用,尽管它被错误地留在了中。它将很快从语言中删除


Python 2.6-3.3 您可以使用标准库中的
HTMLParser.unescape()

  • 对于Python2.6-2.7,它在
  • 对于Python3,它在
您还可以使用兼容性库简化导入:

>>> from six.moves.html_parser import HTMLParser
>>> h = HTMLParser()
>>> print(h.unescape('&pound;682m'))
£682m

Beauty Soup处理实体转换。在Beautiful Soup 3中,您需要为
BeautifulSoup
构造函数指定
convertEntities
参数(请参阅归档文档的一节)。在BeautifulSoup4中,实体会自动解码

靓汤3
>>从BeautifulSoup导入BeautifulSoup
>>>美丽集团(“6.82亿英镑”),
…convertEntities=BeautifulSoup.HTML_ENTITIES)
6.82亿英镑

靓汤4
>>来自bs4导入组
>>>美丽集团(“6.82亿英镑”)
6.82亿英镑


这可能与此处无关。但是,要从整个文档中删除这些html元素,您可以这样做:(假设document=page,请原谅这些草率的代码,但是如果您对如何使其更好有想法,我洗耳恭听——我对此一无所知)


美丽的汤4可以让你

如果传入
formatter=None
,Beautiful Soup将不会修改字符串 完全依赖于输出。这是最快的选择,但可能会导致 Beauty Soup生成无效的HTML/XML,如以下示例所示:

打印(soup.prettify(格式化程序=无))
# 
#  
#
#伊拉迪特
#

# # link_soup=beautifulsou(“”) 打印(link_.a.encode(格式化程序=无)) #
您可以使用w3lib.html库中的替换实体

In [202]: from w3lib.html import replace_entities

In [203]: replace_entities("&pound;682m")
Out[203]: u'\xa3682m'

In [204]: print replace_entities("&pound;682m")
£682m

我也有类似的编码问题。我使用了normalize()方法。在将数据帧导出到另一个目录中的.html文件时,我使用pandas.to_html()方法遇到了一个Unicode错误。我最终做了这件事,它成功了

    import unicodedata 
dataframe对象可以是您喜欢的任何对象,让我们称之为table

    table = pd.DataFrame(data,columns=['Name','Team','OVR / POT'])
    table.index+= 1
对表数据进行编码,以便我们可以将其导出到templates文件夹中的out.html文件(可以是您希望的任何位置:)

将规范化字符串导出到html文件

    file = open("templates/home.html","w") 

    file.write(html_data) 

    file.close() 

参考资料:

+1。不知道我怎么会在文档中漏掉这个:谢谢你的信息。我将接受luc的回答tho,因为他使用的是我在问题中指定的标准库(对我来说并不重要),而且它可能对其他人更通用。
BeautifulSoup4
主要使用
HTMLPasser
。请参见如何在BeautifulSoup4中获得转换,而不使用所有不属于原始字符串的无关HTML?(即和)@Praxiteles:BeautifulSoup(“£;682m”,“html.parser”)此方法在google app engine上似乎不会转义“’;”等字符,尽管它在python2.6上本地工作。它仍然解码实体(如“)至少有哪些未记录的API可以被弃用?编辑答案。@MarkusInterwaditzer没有理由不弃用未记录的方法。这一个会抛出弃用警告-请参阅我对答案的编辑。与其使用
unescape
方法,还不如使用整个
HTMLParser
模块不推荐使用
html.parser
。Python 2值得注意的是:特殊字符被替换为拉丁-1(ISO-8859-1)编码的对应字符。例如,可能需要
h.unescape.encode(“utf-8”)
。文档:“此处提供的定义包含XHTML 1.0定义的所有实体,这些实体可以使用拉丁字符集(ISO-8859-1)“”中的简单文本替换进行处理。相关:否!您不需要自己匹配HTML实体并循环它们;
.unescape()
为您这样做。我不明白为什么您和Rob发布了这些过度复杂的解决方案,这些解决方案在接受的答案已经清楚地表明
.unescape()
可以在字符串中找到实体的情况下滚动自己的实体匹配。这并不能回答问题。(另外,我不知道文档所说的关于HTML的最后一部分是无效的。)是无效的部分,因为它已经取消了,并将破坏它周围的HTML。我知道这是我的迟发帖子,但万一有人碰巧看到并想知道。。。
import re
import HTMLParser

regexp = "&.+?;" 
list_of_html = re.findall(regexp, page) #finds all html entites in page
for e in list_of_html:
    h = HTMLParser.HTMLParser()
    unescaped = h.unescape(e) #finds the unescaped value of the html entity
    page = page.replace(e, unescaped) #replaces html entity with unescaped value
print(soup.prettify(formatter=None))
# <html>
#  <body>
#   <p>
#    Il a dit <<Sacré bleu!>>
#   </p>
#  </body>
# </html>

link_soup = BeautifulSoup('<a href="http://example.com/?foo=val1&bar=val2">A link</a>')
print(link_soup.a.encode(formatter=None))
# <a href="http://example.com/?foo=val1&bar=val2">A link</a>
In [202]: from w3lib.html import replace_entities

In [203]: replace_entities("&pound;682m")
Out[203]: u'\xa3682m'

In [204]: print replace_entities("&pound;682m")
£682m
    import unicodedata 
    table = pd.DataFrame(data,columns=['Name','Team','OVR / POT'])
    table.index+= 1
     #this is where the magic happens
     html_data=unicodedata.normalize('NFKD',table.to_html()).encode('ascii','ignore')
    file = open("templates/home.html","w") 

    file.write(html_data) 

    file.close()