UnicodeDammit 是BS内置库, 主要用来猜测文档编码.

编码自动检测 功能可以在Beautiful Soup以外使用,检测某段未知编码时,可以使用这个方法:

from bs4 import UnicodeDammit
dammit = UnicodeDammit("Sacr\xc3\xa9 bleu!")
print(dammit.unicode_markup)
# Sacré bleu!
dammit.original_encoding
# 'utf-8'

如果Python中安装了 chardet 或 cchardet 那么编码检测功能的准确率将大大提高. 输入的字符越多,检测结果越精确,如果事先猜测到一些可能编码, 那么可以将猜测的编码作为参数,这样将优先检测这些编码:

dammit = UnicodeDammit("Sacr\xe9 bleu!", ["latin-1", "iso-8859-1"])
print(dammit.unicode_markup)
# Sacré bleu!
dammit.original_encoding
# 'latin-1'

智能引号

使用Unicode时,Beautiful Soup还会智能的把引号, 转换成HTML或XML中的特殊字符:

markup = b"<p>I just \x93love\x94 Microsoft Word\x92s smart quotes</p>"

UnicodeDammit(markup, ["windows-1252"], smart_quotes_to="html").unicode_markup
# u'<p>I just &ldquo;love&rdquo; Microsoft Word&rsquo;s smart quotes</p>'

UnicodeDammit(markup, ["windows-1252"], smart_quotes_to="xml").unicode_markup
# u'<p>I just “love” Microsoft Word’s smart quotes</p>也可以把引号转换为ASCII码:
UnicodeDammit(markup, ["windows-1252"], smart_quotes_to="ascii").unicode_markup
# u'<p>I just "love" Microsoft Word\'s smart quotes</p>'

很有用的功能,但是Beautiful Soup没有使用这种方式.默认情况下,Beautiful Soup把引号转换成Unicode:
UnicodeDammit(markup, ["windows-1252"]).unicode_markup
# u'<p>I just \u201clove\u201d Microsoft Word\u2019s smart quotes</p>'


UnicodeDammit的更多相关文章

  1. BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

    BeautifulSoup很赞的东西 最近出现一个问题:Python 3.3 soup=BeautifulSoup(urllib.request.urlopen(url_path),"htm ...

  2. 转:Beautiful Soup

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...

  3. python 模块BeautifulSoup使用

    BeautifulSoup是一个专门用于解析html/xml的库.官网:http://www.crummy.com/software/BeautifulSoup/ 说明,BS有了4.x的版本了.官方说 ...

  4. bs4源码

    Beautiful源码: """Beautiful Soup Elixir and Tonic "The Screen-Scraper's Friend&quo ...

  5. Beautifulsoup官方文档

    Beautiful Soup 中文文档 原文 by Leonard Richardson (leonardr@segfault.org) 翻译 by Richie Yan (richieyan@gma ...

  6. Beautiful Soup 学习手册

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式   快速开始 下面的一段HTML代码将作为例 ...

  7. python爬虫 beutifulsoup4_1官网介绍

    http://www.crummy.com/software/BeautifulSoup/bs4/doc/ Beautiful Soup Documentation Beautiful Soup is ...

  8. python之BeautifulSoup模块

    # 名称修改(bs4) from bs4 import BeautifulSoup 帮助文档 Beautiful Soup parses a (possibly invalid) XML or HTM ...

  9. beautifulsoup之CSS选择器

    BeautifulSoup支持大部分的CSS选择器,其语法为:向tag或soup对象的.select()方法中传入字符串参数,选择的结果以列表形式返回. tag.select("string ...

随机推荐

  1. jQuery双击编辑td数据

    html <td class="remark" style="width: 200px;"> {$vo.remark} </td> js ...

  2. java数组声明和变式--record1

    ​ java声明数组方式: String[] namelist; int numlist[];//此声明为动态声明,不能指定长度,numlist[10] 静态声明的方式: int a[]={1,2,3 ...

  3. http和WebSocket

    有关http和WebSocket 的区别网上有很多的质料. 个人在此仅仅是记录以下自己的学习心得,自己的理解. 1. http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要 ...

  4. 第一次学习 CG( c for graphic) 遇到的一大推坑

    1.CG开发环境的配置: 具体的工具包下载及整体的配置过程可以参考:https://blog.csdn.net/seamanj/article/details/8300936. 上面网址的内容是对VS ...

  5. English trip -- VC(情景课) 7 D Reading 阅读练习

    Read Hi patty, This morning, Smuel and I are going to The Clothes place. Samuel needs blue pants.He ...

  6. 3-15 《元编程》第6章 3-16 hook method

    Code That Writes Code 6.1 Coding your way to the weekend 6.2 Kernel#eval, Binding#eval Binding: Obje ...

  7. C++面试问题详解

    1.定义一个全局变量放在.cpp文件还是.h文件,原因是什么 在cpp文件中定义变量,h文件用来声明变量的作用域,使用extern声明的变量可以在本编译单元或其他编译单元中使用. 举例如下: a.h文 ...

  8. Android studio的 repositories配置多个url

    buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2. ...

  9. SSH 本地端口转发

    有时,绑定本地端口还不够,还必须指定数据传送的目标主机,从而形成点对点的"端口转发".为了区别后文的"远程端口转发",我们把这种情况称为"本地端口转发 ...

  10. java反编译工具eclipse插件jad的使用

    https://www.cnblogs.com/zhikou/p/8098137.html 这边文章主要介绍如何使用Eclipse的插件jad进行反编译,在查看一些jar包里面的class文件时,就可 ...