Python实现抓取CSDN首页文章列表

1.使用工具:

2.抓取网站:
csdn首页文章列表 http://blog.csdn.net/
3、实现代码:

4.执行脚本

继续阅读Python实现抓取CSDN首页文章列表

UTF8 + BOM产生问题与小结

写python脚本的时候发现这样一个问题:从xls文件导出到txt时,无法直接转换为int型数据,输出查看发现和文件编码方式产生的附加信息有关用一个简单的文件举例

当文件分别用ascii,utf8,utf8+bom作为编码格式时,显示输出结果如下:

使用ascii编码的输出:

使用utf8编码的输出:

使用bom编码的输出:

原来utf8+bom不能直接转换int的原因在这里,它在文件头插入了一个表示文件编码的信息\xef\xbb\xbf,那么UTF-8(无BOM)和UTF-8这两个有什么区别呢?BOM是什么呢?

什么是BOM?

BOM: Byte Order Mark

UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支持UTF-16,UTF-32才加上的

BOM,BOM签名的意思就是告诉编辑器当前文件采用何种编码,方便编辑器识别,但是BOM虽然在编辑器中不显示,但是会产生输出,就像多了一个空行。

Byte Order Marks are special characters at the beginning of a Unicode file to indicate whether it is big or little endian, in other words does the high or low order byte come first. These codes also tell whether the encoding is 8, 16 or 32 bit. You can recognise Unicode files by their starting byte order marks, and by the way Unicode-16 files are half zeroes and Unicode-32 files are three-quarters zeros. Unicode Endian Markers

Byte-order mark Description
EF BB BF UTF-8
FF FE UTF-16 aka UCS-2, little endian
FE FF UTF-16 aka UCS-2, big endian
00 00 FF FE UTF-32 aka UCS-4, little endian.
00 00 FE FF UTF-32 aka UCS-4, big-endian.

UTF的字节序和BOM

UTF- 8以字节为编码单元,没有字节序的问题。UTF-16以两个字节为编码单元,在解释一个UTF-16文本前,首先要弄清楚每个编码单元的字节序。例如收到 一个“奎”的Unicode编码是594E,“乙”的Unicode编码是4E59。如果我们收到UTF-16字节流“594E”,那么这是“奎”还是 “乙”?

Unicode规范中推荐的标记字节顺序的方法是BOM。BOM不是“Bill Of Material”的BOM表,而是Byte Order Mark。BOM是一个有点小聪明的想法:

在 UCS编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输 字符"ZERO WIDTH NO-BREAK SPACE"。

这样如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little-Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。

UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。

Windows就是使用BOM来标记文本文件的编码方式的。

原来BOM是在文件的开始加了几个字节作为标记。有了这个标记,一些协议和系统才能识别。

ok,说了这么多背景,那么如何解决这个问题呢?

如何使用BOM头

BOM头的删除

对UTF-16, Python将BOM解码为空字串。然而对UTF-8, BOM被解码为一个字符,如例:

简单的做法是在文件读入时使用

即可,具体可以参见[http://docs.python.org/library/codecs.html#module-encodings.utf_8_sig|http://docs.python.org/library/codecs.html#module-encodings.utf_8_sig]

或者:

BOM头的添加

参考 http://www.cnblogs.com/DDark/archive/2011/11/28/2266085.html

python下的编码检测——chardet

在处理字符串时,常常会遇到不知道字符串是何种编码,如果不知道字符串的编码就不能将字符串转换成需要的编码。面对多种不同编码的输入方式,是否会有一种有效的编码方式?chardet是一个非常优秀的编码识别模块。

chardet 是python的第三方库,需要下载和安装。现在 pip 已经可以很好的支持这个版本的下载了,建议使用pip 安装,关于pip 的安装部分,可以参考Windows下python的包管理器pip安装
在安装完chardet模块,我就可以使用它了,来看一段示例代码。
运行结果表示有99%的概率认为这段代码是GB2312编码方式。
另外一个相对高级的应用。

应用背景,如果要对一个大文件进行编码识别,使用这种高级的方法,可以只读一部,去判别编码方式从而提高检测速度。

参考 http://blog.csdn.net/aqwd2008/article/details/7506007

Windows下python的包管理器pip安装

做python开发,要用到第三方包,关于MAC下面如何安装Pip ,可以参考前面的 Mac OS 10.9 下python安装easy_install pip 现在我们看看在Windows下面的操作,打开 Pip的官方安装指南https://pip.pypa.io/en/latest/installing.html

Installation

Python & OS Support

pip works with CPython versions 2.6, 2.7, 3.1, 3.2, 3.3, 3.4 and also pypy.

pip works on Unix/Linux, OS X, and Windows.

Note

Python 2.5 was supported through v1.3.1, and Python 2.4 was supported through v1.1.

Install pip

To install or upgrade pip, securely download get-pip.py.(如果官网下载不下来,可以点这个链接在本网站下载

Then run the following (which may require administrator access):

If setuptools (or distribute) is not already installed, get-pip.py will install setuptools for you. [2]

To upgrade an existing setuptools (or distribute), run pip install -U setuptools. [3]

To enable the use of pip from the command line, ensure the Scripts subdirectory of your Python installation is available on the system PATH. (This is not done automatically.)

Additionally, get-pip.py supports using the pip install options and the general options. Below are some examples:

Install from local copies of pip and setuptools:

Install to the user site [4]:

Install behind a proxy:

Upgrade pip

On Linux or OS X:

On Windows [5]:

Using Package Managers

On Linux, pip will generally be available for the system install of python using the system package manager, although often the latest version will be unavailable.

On Debian and Ubuntu:

On Fedora:

python在windows的cmd中打印彩色文字