UnicodeDammit
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 “love” Microsoft Word’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的更多相关文章
- 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 ...
- 转:Beautiful Soup
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...
- python 模块BeautifulSoup使用
BeautifulSoup是一个专门用于解析html/xml的库.官网:http://www.crummy.com/software/BeautifulSoup/ 说明,BS有了4.x的版本了.官方说 ...
- bs4源码
Beautiful源码: """Beautiful Soup Elixir and Tonic "The Screen-Scraper's Friend&quo ...
- Beautifulsoup官方文档
Beautiful Soup 中文文档 原文 by Leonard Richardson (leonardr@segfault.org) 翻译 by Richie Yan (richieyan@gma ...
- Beautiful Soup 学习手册
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 快速开始 下面的一段HTML代码将作为例 ...
- python爬虫 beutifulsoup4_1官网介绍
http://www.crummy.com/software/BeautifulSoup/bs4/doc/ Beautiful Soup Documentation Beautiful Soup is ...
- python之BeautifulSoup模块
# 名称修改(bs4) from bs4 import BeautifulSoup 帮助文档 Beautiful Soup parses a (possibly invalid) XML or HTM ...
- beautifulsoup之CSS选择器
BeautifulSoup支持大部分的CSS选择器,其语法为:向tag或soup对象的.select()方法中传入字符串参数,选择的结果以列表形式返回. tag.select("string ...
随机推荐
- MongoDB(课时22 过期索引)
3.6.2 过期索引 在一些程序的站点会出现若干秒之后信息被删除的情况,例如:手机信息验证码,那么在MongoDB里面可以轻松实现过期索引.但这个时间往往不怎么准确. 范例:设置过期索引(实现过期索引 ...
- python json格式转xml格式
import xmltodict #json转xml函数 def jsontoxml(jsonstr): #xmltodict库的unparse()json转xml xmlstr = xmltodic ...
- [ios][map]自定义地图标注
参考:http://blog.csdn.net/mad1989/article/details/8794762 ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高 ...
- 统计字符串中每个字符出现的次数(Python)
#统计字符串中每个字符出现的次数 以The quick brown fox jumps over the lazy dog为例 message='The quick brown fox jumps o ...
- Lua面向对象 --- 多继承
工程目录结构: ParentMother.lua: ParentMother = {} function ParentMother:MortherName() print("Morther ...
- 流氓 2345.com的新动态及解决方法
安装了[电脑公司]的Win7_SP1之后, IE的主页被绑架. 症状是先转到 IE959.com,然后自动跳转到 www.2345.com 网上当然有很多例子了,可是都没有效果. 1. 更改IE设置没 ...
- ehcache入门基础示例
一:目录 EhCache 简介 Hello World 示例 Spring 整合 二: 简介 1. 基本介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernat ...
- 读CSV转换datatable
using System.Data; using System.IO; /// <summary> /// Stream读取.csv文件 /// </summary> // ...
- 3-18/19 (自我练习)30多个《Ruby元编程》的spell(pattern)小例子。
Spell,也称pattern,idiom # Around Alias:从一个重新定义的方法中调用原始的,被重命名的版本. # old_reverse是未改变的原始方法,reverse/new_re ...
- fzu1901 kmp
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...