Beautiful Soup处理unicode
在python众多的辅助脚本中,beautiful soup(美丽的汤)无疑是耀眼的一个。
BS解析HTML,XML相当方便,但其中还是有一些问题的,尤其是在处理unicode的问题上,其实这也不能怪BS,毕竟python的unicode解决方案就不是那么的优雅,当然pyython 3.0就好多了。
由于BS在解析HTML时,要把所有内容全部转换成unicode来进行存储,这就出现了一个问题,BS怎么判断原始的HTML文件的编码方式?
BS根据以下规则来判断:
* 可以通过fromEncoding参数传递编码类型给soup的构造器
* 通过文档本身找到编码类型:例如XML的声明或者HTML文档http-equiv的META标签。如果Beautiful Soup在文档中发现编码类型,它试着使用找到的类型转换文档。但是,如果你明显的指定一个编码类型,并且成功使用了编码:这时它会忽略任何它在文档中发现的编码类型。
* 通过嗅探文件开头的一下数据,判断编码。如果编码类型可以被检测到,它将是这些中的一个:UTF-*编码,EBCDIC或者ASCII。
* 通过chardet 库,嗅探编码,如果你安装了这个库。
* UTF-8
* Windows-1252
* An encoding you pass in as the fromEncoding argument to the soup constructor.
* An encoding discovered in the document itself: for instance, in an XML declaration or (for HTML documents) an http-equiv META tag. If Beautiful Soup finds this kind of encoding within the document, it parses the document again from the beginning and gives the new encoding a try. The only exception is if you explicitly specified an encoding, and that encoding actually worked: then it will ignore any encoding it finds in the document.
* An encoding sniffed by looking at the first few bytes of the file. If an encoding is detected at this stage, it will be one of the UTF-* encodings, EBCDIC, or ASCII.
* An encoding sniffed by the chardet library, if you have it installed.
* UTF-8
* Windows-1252
这并不能保证结果的正确性,所以,在BS不能正确判断的时候,需要使用者指定当前HTML页面的编码方式。
例如:
soup = BeautifulSoup(gbk, fromEncoding="gbk")
这样就ok了
Django自定义tag
自定义的tag在重复显示的时候真是有用,赞一个:)
浪点虚拟主机上装django
参考:
http://www.phppear.com/index.php?m=03&y=08&entry=entry080328-113652
1. 开通ssh服务
2. 从这里
http://peak.telecommunity.com/dist/virtual-python.py
http://peak.telecommunity.com/dist/ez_setup.py
分别下载 virtual-python.py和ez_setup.py
在主机上建立一个evn文件夹并把这两个文件上传进去
3. 执行python virtual-python.py
4. vim ~/.bashrc
翻到结尾,在最后加上
alias python=~/bin/python
5. 重新登录shell
切换到evn目录下面
运行
python ez_setup.py
来安装setuptools
6. 在django.fcgi的第一行写成
#!/var/www/virtualhost/浪点用户名/bin/python
7.pkill django.fcgi
8.转到要安装的django的目录下,python setup.py install
okay,接下来跑的就是自己版本的django了

