13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/1/13
pyquery:基于python和jquery语法操作XML
pyquery可以让你用jquery语法来对xml进行查询。这个API和jquery十分类似。如果利用
lxml,pyquery对xml和html的操作将更加快速。
这个库并不是(至少还不是)一个可以和javascript互交的代码库。只是很喜欢jqueryAPI
并且在使用python的过程中,我真的很怀念jquery,所以我告诉我自己“让我们在
python里面也使用jquery吧!”所以就有了这个库。
这个库可以有多种用途,比如我可以在将来用pyquery对纯http模板就行编辑,或者可以
和Deliverance配套使用对样式进行操作.
这个项目现在基于mercurial开发,并用Bitbucket发布。我有权给任何想要审查代码的人
权利。如果你想对代码进行贡献,给我电邮吧。
欢迎大家访问我的网站geoinformatics.cn.
内容
pyquery:基于python和jquery语法操作XML
用法
属性(Attributes)
样式表(CSS)
转换(Traversing)
操作(Manipulating)
AJAX
生成绝对链接
使用不同的解析器
测试
更多的文档
TODO
pyquery.pyquery–PyQuery完全API手册
pyquery.ajax–PyQueryAJAX扩展
用法
用户可以使用PyQuery类从字符串、lxml对象、文件或者url来加载xml文档:
>>>frompyqueryimportPyQueryaspq
>>>fromlxmlimportetree
>>>d=pq("")
>>>d=pq(etree.fromstring(""))
>>>d=pq(url=''http://google.com/'')
>>>d=pq(filename=path_to_html_file)
d在这里诸如jquery里面的$对象:
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/2/13
>>>d("#hello")
[]
>>>p=d("#hello")
>>>p.html()
''Helloworld!''
>>>p.html("youknowPythonrocks")
[]
>>>p.html()
''youknowPythonrocks''
>>>p.text()
''youknowPythonrocks''
用户可以使用jquery提供的一些伪类(但还不支持css)来进行操作,诸如:first:last:even
:odd:eq:lt:gt:checked:selected:file:
>>>d(''p:first'')
[]
Attributes
YoucanplaywiththeattributeswiththejqueryAPI:
>>>p.attr("id")
''hello''
>>>p.attr("id","plop")
[ ]
>>>p.attr("id","hello")
[ ]
或者使用更加符合python模式的方法:
>>>p.attr.id="plop"
>>>p.attr.id
''plop''
>>>p.attr["id"]="ola"
>>>p.attr["id"]
''ola''
>>>p.attr(id=''hello'',class_=''hello2'')
[ ]
>>>p.attr.class_
''hello2''
>>>p.attr.class_=''hello''
样式表(CSS)
你同时也可以对css进行操作:
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/3/13
>>>p.addClass("toto")
[ ]
>>>p.toggleClass("tititoto")
[ ]
>>>p.removeClass("titi")
[ ]
或者是css模式:
>>>p.css("font-size","15px")
[ ]
>>>p.attr("style")
''font-size:15px''
>>>p.css({"font-size":"17px"})
[ ]
>>>p.attr("style")
''font-size:17px''
或者类似python模式(‘_’符号用‘-‘代替):
>>>p.css.font_size="16px"
>>>p.attr.style
''font-size:16px''
>>>p.css[''font-size'']="15px"
>>>p.attr.style
''font-size:15px''
>>>p.css(font_size="16px")
[ ]
>>>p.attr.style
''font-size:16px''
>>>p.css={"font-size":"17px"}
>>>p.attr.style
''font-size:17px''
转换(Traversing)
支持大部分jQuwey转换方法。这里是一些实例。
用户可以用字符选择器来进行过滤:
>>>d(''p'').filter(''.hello'')
[ ]
也可以对单一元素使用eq方法:
>>>d(''p'').eq(0)
[ ]
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/4/13
用户也可以寻找内嵌元素:
>>>d(''p'').find(''a'')
[,]
>>>d(''p'').eq(1).find(''a'')
[]
>>>d(''p'').find(''a'').end()
[, ]
>>>d(''p'').eq(0).end()
[ , ]
>>>d(''p'').filter(lambdai:i==1).end()
[ , ]
操作(Manipulating)
用户可以给标签最好添加内容。:
>>>d(''p'').append(''checkoutreddit '')
[ , ]
>>>printd
...
youknowPythonrockscheckoutreddit
hellopython!
checkoutPythonrockscheckoutreddit
...
或者最前面:
>>>p.prepend(''checkoutreddit'')
[]
>>>p.html()
''checkoutreddityouknow...''
前置或者椎间一个元素到另一个中:
>>>p.prependTo(d(''#test''))
[]
>>>d(''#test'').html()
''...hello...python...''
插入一个元素:
>>>p.insertAfter(d(''#test''))
[]
>>>d(''#test'').html()
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/5/13
''python!...''
或者在前面:
>>>p.insertBefore(d(''#test''))
[]
>>>d(''body'').html()
''\n...''
对每一个元素进行操作:
>>>p.each(lambdae:e.addClass(''hello2''))
[]
删除元素:
>>>d.remove(''p#id'')
[]
>>>d(''p#id'')
[]
元素替换:
>>>p.replaceWith('' testing '')
[]
>>>d(''p'')
[ , ]
或者另一种方式:
>>>d('' aryastark'').replaceAll(''p'')
[]
>>>d(''p'')
[]
>>>d(''h1'')
[,]
删除选择器里面的内容:
>>>d(''h1'').empty()
[,]
用户也可以获得其中的内容:
>>>printd
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/6/13
用户可以生成html:
>>>frompyqueryimportPyQueryaspq
>>>printpq(''Yeah! '').addClass(''myclass'')+pq(''cool'')
Yeah!cool
AJAX
用户可以查询wsgi应用,只要安装了WebOb(这并不是pyquery所必需的)。在这个例子
中,测试应用将返回输入的对象/以及提交的按钮/submit:
>>>d=pq('''',app=input_app)
>>>d.append(d.get(''/''))
[
在其他节点也可以使用该应用:
>>>d.get(''/'').appisd.appisd(''form'').app
True
用户也可以请求其他的路径:
>>>d.append(d.get(''/submit''))
[
如果安装了Paste,你可以使用Proxy直接获得url所对应的应用:
>>>a=d.get(''https://bitbucket.org/olauzanne/pyquery/'')
>>>a
[]
用户可以获得其返回值:
>>>printa.response.status
301MovedPermanently
返回的属性是一个WebOb返回值
生成绝对链接
用户可以生成绝对链接,这在抓屏过程中很有效:
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/7/13
>>>d=pq(url=''http://www.w3.org/'',parser=''html'')
>>>d(''a[title="W3CActivities"]'').attr(''href'')
''/Consortium/activities''
>>>d.make_links_absolute()
[]
>>>d(''a[title="W3CActivities"]'').attr(''href'')
''http://www.w3.org/Consortium/activities''
使用不同的解析器
pyquery默认使用lxml.xml作为解析器,所以如果用户的应用不能使用,则可以尝试用
lxml.html进行html解析。xml解析器有时候会有些问题。特别是当处理xhtml页面的时
候,因为解析器会触发一个错误当遇到一个没有的xml树时(以w3c.org为例)。
你也可以选择特定的解析器:
>>>pq(''toto '',parser=''xml'')
[]
>>>pq(''toto '',parser=''html'')
[]
>>>pq(''toto '',parser=''html_fragments'')
[]
html和html_fragments解析器来自lxml.html。
测试
如果哦你想运行测试,你可以看到以上内容,遵循以下步骤:
$hgclonehttps://bitbucket.org/olauzanne/pyquery/
$cdpyquery
$pythonbootstrap.py
$bin/buildout
$bin/test
遵循以下步骤来构建Sphinx文档:
$cddocs
$makehtml
如果你没有安装lxml,请使用如下命令:
$STATIC_DEPS=truebin/buildout
更多文档
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/8/13
FirstthereistheSphinxdocumentation点击此处.更多关于该API的文档,请使用
jquery网站.我现在参考书写API来自colorcheatsheet.你随时可以参考源代码。
TODO
选择器(SELECTORS):仍然缺少的jQuery伪类(:radio,:password,...)
属性(ATTRIBUTES):完成
样式表(CSS):完成
HTML:done
操作(MANIPULATING):仍然缺少wrapAll和wrapInner方法。
转换(TRAVERSING):以完成大多半。
事件:还没有和服务器端进行交互,以后可以用ajax
核心UI效果:实现了hide和show。其他和服务器端交互不相干
AJAX:和wsgi应用配合使用的部分
pyquery.pyquery–PyQuery完全API
classpyquery.pyquery.PyQuery(args,kwargs)
主要的类
addClass(value)
给元素添加css类:
>>>d=PyQuery('' '')
>>>d.addClass(''myclass'')
[]
after(value)
给节点添加值
append(value)
给每个节点添加值
appendTo(value)
给值添加节点
attr
支持set/get/del属性,类似javascript样式。
base_url
返回当前html文档的url,如果不存在则返回None。
before(value)
在节点前插入值
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/9/13
clone()
css
支持set/get/del属性,类似javascript样式。
each(func)
给每个节点添加func。
empty()
删除节点内容。
end()
退出当前转换并返回上一层。
>>>m=''Whoah! there ''
>>>d=PyQuery(m)
>>>d(''p'').eq(1).find(''em'').end().end()
[, ]
eq(index)
返回被检索的元素。
>>>d=PyQuery(''Hi Bye '')
>>>d(''p'').eq(0)
[]
>>>d(''p'').eq(1)
[]
filter(selector)
运用(字符串或者函数)进行过滤.
>>>d=PyQuery(''Hi Bye '')
>>>d(''p'')
[,]
>>>d(''p'').filter(''.hello'')
[ ]
>>>d(''p'').filter(lambdai:i==1)
[]
>>>d(''p'').filter(lambdai:PyQuery(this).text()==''Hi'')
[ ]
find(selector)
运用选择器来对元素进行查找。
>>>m=''Whoah! there ''
>>>d=PyQuery(m)
>>>d(''p'').find(''em'')
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/10/13
[,]
>>>d(''p'').eq(1).find(''em'')
[]
hasClass(name)
如果拥有该class则返回True:
>>>d=PyQuery('''')
>>>d.hasClass(''myclass'')
True
height(value=)
设置或获取元素的高度值
hide()
给display添加none于元素样式
html(value=)
设置或者获取子节点html的表达方法。
获取文本值:
>>>doc=PyQuery(''toto '')
>>>printdoc.html()
toto
设置文本值:
>>>doc.html(''Youhou!'')
[] >>>printdoc Youhou! insertAfter(value) 在值后面添加节点 insertBefore(value) 在值前添加节点 is_(selector) 如果选择器符合当前元素则返回True,否则返回False。>>>d= PyQuery(‘ HiBye ’)>>>
d(‘p’).eq(0).is_(‘.hello’)True>>>d(‘p’).eq(1).is_(‘.hello’)False
make_links_absolute(base_url=None)
生成绝对链接。
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/11/13
map(func)
当用户完成转换后,返回一个新的PyQuery对象。
func需要两个参数-‘index’和‘element’.在func里面,元素可以用
‘this’代替。
>>>d=PyQuery(''HithereBye '')
>>>d(''p'').map(lambdai,e:PyQuery(e).text())
[''Hithere'',''Bye'']
>>>d(''p'').map(lambdai,e:len(PyQuery(this).text()))
[8,3]
>>>d(''p'').map(lambdai,e:PyQuery(this).text().split())
[''Hi'',''there'',''Bye'']
not_(selector)
返回不符合选择器的元素。
>>>d=PyQuery(''HiBye '')
>>>d(''p'').not_(''.hello'')
[]
prepend(value)
给节点前附加一个值
prependTo(value)
给值前附加一个节点。
remove(expr=)
删除节点。
removeAttr(name)
删除一个属性:
>>>d=PyQuery('' '')
>>>d.removeAttr(''id'')
[] removeClass(value) 删除一个元素的css类 >>>d=PyQuery('' '')
>>>d.removeClass(''myclass'')
[] 13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档 geoinformatics.cn/lab/pyquery/12/13 replaceAll(expr) 用表达式替换节点。 replaceWith(value) 用值替换节点。 show() 将元素的display属性更变为block。 text(value= )
设置或者获得子元素的文本。
获得文本值:
>>>doc=PyQuery(''tototata '')
>>>printdoc.text()
tototata
设置文本值:
>>>doc.text(''Youhou!'')
[] >>>printdoc Youhou! toggleClass(value) 开关元素的某个css类 >>>d=PyQuery('' '') >>>d.toggleClass(''myclass'') [ ]
val(value=)
设置或者获得元素值:
>>>d=PyQuery('''')
>>>d.val(''Youhou'')
[]>>>d.val()‘Youhou’
width(value=)
set/getwidthofelement
wrap(value)
每个对象将实时添加HTML字符串:
>>>d=PyQuery(''youhou'')
13-5-9pyquery:基于python和jquery语法操作XML—pyqueryv0.3文档
geoinformatics.cn/lab/pyquery/13/13
>>>d.wrap('''')
[] >>>printd youhou wrapAll(value) 将所有的元素包装到一个元素中: >>>d=PyQuery('' Heyyou! '') >>>printd(''span'').wrapAll('' '')
Heyyou!
pyquery.ajax–PyQueryAJAX扩展
classpyquery.ajax.PyQuery(args,kwargs)
get(path_info,kwargs)
获取wsgi应用或者url的路径。
post(path_info,kwargs)
从wsgi应用或者url来POST某个路径。
|