IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

关于使用python开发web应用的几个库总结

Vimer 2010-07-20 23:20:20 累计浏览 8,540 次
本机暂存

怎么说呢,python可以算是救了我一命的一门语言,若不是他,恐怕公司的这个项目还遥遥无期,我会始终保有对他最崇高的敬意。

这里,我仅列出自己最近使用的几个库,并简单演示一下其高超的使用效率。

1.jinja2
简介:一个模板替换类,衍生于django的Template,在很多地方做了加强。
中文支持:如果render渲染失败,请先将想要渲染的内容decode成对应编码。
备注:jinja2在renderXML等格式的文件的时候,不会自动替换html标签,这点有时候是好事~~,因为我并不用jinjia直接来往前台吐页面。
使用:
使用方法很简单:

from jinja2 import Template
template = Template('Hello {{ name }}!')
print template.render(name='World')

也支持不定函数变量的传递:

from jinja2 import Template
template = Template(''
'
    {{ name }}!
    {%if test0 > 8%}{{test1}} is larger ! {%endif%}
    {%if test1 > 8%}{{test1}} is larger ! {%endif%}
    {%if test2 > 8%}{{test2}} is larger ! {%endif%}
        '
'')
alls = [8,9,10]
mapAll = {}
for i, ch in enumerate(alls):
    mapAll["test"+str(i)] = ch
print template.render(name='Hello!',**mapAll)

输出为:

Hello!! 
9 is larger ! 
10 is larger !

2.simplejson
简介:一个把python数据结构和json互相转化的类
中文支持:
支持,但是当需要dumps的数据中含有unicode字符的时候,需要指定ensure_ascii = False,如下:

outdata = simplejson.dumps(pydata,ensure_ascii = False)

备注:无
使用:

from django.utils import simplejson
strdata = simplejson.dumps({'ret':1})
print strdata
pydata = simplejson.loads(strdata)
print pydata['ret']

输出为:

{"ret": 1}
1

3.BeautifulSoup
简介:一个非常与众不同的解析HTML/XML的类库,用起来非常快捷,方便
中文支持:很好
备注:本博的糗事百科的vim插件就是用它来解析的html
使用:
假设待解析的数据如下(存在data中):

#<?xml version='1.0' encoding='gb2312' ?>                                                           
#<root>
#    <domain name='appbasesh.qzone.qq.com' operation='update'>
#        <ip inner='10.149.24.156' outer='180.153.2.222' isp='ctc' type='inner_ip'/>
#        <ip inner='10.150.16.29' outer='112.64.199.30' isp='cnc' type='com_proxy'/>
#        <ip inner='10.149.24.157' outer='180.153.2.223' isp='ctc' type='inner_ip'/>
#        <ip inner='10.149.24.158' outer='180.153.2.240' isp='ctc' type='inner_ip'/>
#        <ip inner='10.149.24.159' outer='180.153.2.241' isp='ctc' type='inner_ip'/>
#        <ip inner='10.149.24.160' outer='180.153.2.242' isp='ctc' type='inner_ip'/>
#    </domain>
#</root>
soup = BeautifulSoup(data,convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
iplist = []
#获取所有包含属性type="inner_ip"的节点
allTags = soup.findAll(attrs={'type' : 'inner_ip'})
for tag in allTags:
    #获取属性列表
    for at in tag.attrs:
        #输出第一个属性的内容
        print at[0]

4.minidom
简介:生成XML的工具
中文支持:支持,但是需要如下注意:
1.minidom的设置属性的方法是setAttribute,提供两个参数,一个属性名,一个属性值。之前在文章中介绍过,即当两个字符串相连时,encode/decode必须一致。
2.当minidom输出时,如果想指定xml头的encode为utf-8,可以如下写:
print doc.toprettyxml(encoding='UTF-8')
但是这里的要求就是,如果指定了encoding,那么传入minidom的字符串都应该decode成对应encoding的。而且这样toprettyxml输出的结果是encode过的。
备注:无
使用:

doc = minidom.Document()
item_info = doc.createElement("item_info")
item_info.setAttribute('maxid','90012')
doc.appendChild(item_info)
for o in mman_objs:
    item = doc.createElement("item")
    item_info.appendChild(item)
    for k in o.keys():
        attrdata = unicode(obj[k])
        item.setAttribute(unicode(k),attrdata)
objdatas = doc.toprettyxml(encoding='UTF-8',indent = "    ").decode('utf-8')

OK,这几个工具可以说涵盖了web开发中常用的几种交互方式,HTML/XML,json,模板替换,使用起来如鱼得水啊~

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,252)
  2. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 107,167)
  3. 程序员技术练级攻略 (累计阅读 35,472)
  4. python实现自动登录discuz论坛 (累计阅读 32,834)
  5. python编程细节──遍历dict的两种方法比较 (累计阅读 20,371)
  6. 每个程序员都应该学习使用Python或Ruby (累计阅读 17,919)
  7. Chrome和goagent的配置方法,你懂的 (累计阅读 16,843)
  8. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 13,742)
  9. 使用python爬虫抓站的一些技巧总结:进阶篇 (累计阅读 13,302)
  10. 我的PHP,Python和Ruby之路 (累计阅读 13,149)