烟台百度网站,10m光纤做网站,东莞财务公司代注册公司,新建网站怎么保存你是否觉得 XPath 的用法多少有点晦涩难记呢#xff1f; 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢#xff1f; 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢#xff1f; 你是否已经有了一些前端基础了解选择器却与另外一些奇怪的选择器语法混淆了…你是否觉得 XPath 的用法多少有点晦涩难记呢 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢 你是否已经有了一些前端基础了解选择器却与另外一些奇怪的选择器语法混淆了呢 嗯那么前端大大们的福音来了PyQuery 来了乍听名字你一定联想到了 jQuery如果你对 jQuery 熟悉那么 PyQuery 来解析文档就是不二之选包括我在内 PyQuery 是 Python 仿照 jQuery 的严格实现。语法与 jQuery 几乎完全相同所以不用再去费心去记一些奇怪的方法了。 天下竟然有这等好事我都等不及了
安装
pip install pyquery官方文档
官方文档 pyquery 可让你用 jQuery 的语法来对 xml 进行操作。这 I 和 jQuery 十分类似。如果利用 lxmlpyquery 对 xml 和 html 的处理将更快。 这个库不是至少还不是一个可以和 JavaScript 交互的代码库它只是非常像 jQuery API 而已。
初始化
在这里介绍四种初始化方式。
直接字符串
from pyquery import PyQuery as pq
doc pq(html/html)pq 参数可以直接传入 HTML 代码doc 现在就相当于 jQuery 里面的 $ 符号了。
lxml.etree
from lxml import etree
doc pq(etree.fromstring(html/html))可以首先用 lxml 的 etree 处理一下代码这样如果你的 HTML 代码出现一些不完整或者疏漏都会自动转化为完整清晰结构的 HTML 代码。
直接传 URL
from pyquery import PyQuery as pq
doc pq(http://www.baidu.com)这里就像直接请求了一个网页一样类似用 urllib2 来直接请求这个链接得到 HTML 代码。
传文件
from pyquery import PyQuery as pq
doc pq(filenamehello.html)可以直接传某个路径的文件名。
快速体验
现在我们以本地文件为例传入一个名字为 hello.html 的文件文件内容为
divulli classitem-0first item/lili classitem-1a hreflink2.htmlsecond item/a/lili classitem-0 activea hreflink3.htmlspan classboldthird item/span/a/lili classitem-1 activea hreflink4.htmlfourth item/a/lili classitem-0a hreflink5.htmlfifth item/a/li/ul/div编写如下程序
from pyquery import PyQuery as pq
doc pq(filenamehello.html)
print doc.html()
print type(doc)
li doc(li)
print type(li)
print li.text()ulli classitem-0first item/lili classitem-1a hreflink2.htmlsecond item/a/lili classitem-0 activea hreflink3.htmlspan classboldthird item/span/a/lili classitem-1 activea hreflink4.htmlfourth item/a/lili classitem-0a hreflink5.htmlfifth item/a/li/ulclass pyquery.pyquery.PyQuery
class pyquery.pyquery.PyQuery
first item second item third item fourth item fifth item看回忆一下 jQuery 的语法是不是运行结果都是一样的呢 在这里我们注意到了一点PyQuery 初始化之后返回类型是 PyQuery利用了选择器筛选一次之后返回结果的类型依然还是 PyQuery这简直和 jQuery 如出一辙不能更赞然而想一下 BeautifulSoup 和 XPath 返回的是什么列表一种不能再进行二次筛选在这里指依然利用 BeautifulSoup 或者 XPath 语法的对象 然而比比 PyQuery哦我简直太爱它了
属性操作
from pyquery import PyQuery as pqp pq(p idhello classhello/p)(p)
print p.attr(id)
print p.attr(id, plop)
print p.attr(id, hello)hello
p idplop classhello/
p idhello classhello/from pyquery import PyQuery as pqp pq(p idhello classhello/p)(p)
print p.addClass(beauty)
print p.removeClass(hello)
print p.css(font-size, 16px)
print p.css({background-color: yellow})p idhello classhello beauty/
p idhello classbeauty/
p idhello classbeauty stylefont-size: 16px/
p idhello classbeauty stylefont-size: 16px; background-color: yellow/p 是一直在原来的结果上变化的。 因此执行上述操作之后p 本身也发生了变化。
DOM操作
from pyquery import PyQuery as pqp pq(p idhello classhello/p)(p)
print p.append( check out a hrefhttp://reddit.com/r/pythonspanreddit/span/a)
print p.prepend(Oh yes!)
d pq(div classwrapdiv idtesta hrefhttp://cuiqingcai.comGermy/a/div/div)
p.prependTo(d(#test))
print p
print d
d.empty()
print dp idhello classhello check out a hrefhttp://reddit.com/r/pythonspanreddit/span/a/p
p idhello classhelloOh yes! check out a hrefhttp://reddit.com/r/pythonspanreddit/span/a/p
p idhello classhelloOh yes! check out a hrefhttp://reddit.com/r/pythonspanreddit/span/a/p
div classwrapdiv idtestp idhello classhelloOh yes! check out a hrefhttp://reddit.com/r/pythonspanreddit/span/a/pa hrefhttp://cuiqingcai.comGermy/a/div/div
div classwrap/DOM 操作也是与 jQuery 如出一辙。
遍历
hello.html
divulli classitem-0first item/lili classitem-1a hreflink2.htmlsecond item/a/lili classitem-0 activea hreflink3.htmlspan classboldthird item/span/a/lili classitem-1 activea hreflink4.htmlfourth item/a/lili classitem-0a hreflink5.htmlfifth item/a/li/ul/div遍历用到 items 方法返回对象列表或者用 lambda
from pyquery import PyQuery as pq
doc pq(filenamehello.html)
lis doc(li)
for li in lis.items():print li.html()print lis.each(lambda e: e)first item
a hreflink2.htmlsecond item/a
a hreflink3.htmlspan classboldthird item/span/a
a hreflink4.htmlfourth item/a
a hreflink5.htmlfifth item/a
li classitem-0first item/lili classitem-1a hreflink2.htmlsecond item/a/lili classitem-0 activea hreflink3.htmlspan classboldthird item/span/a/lili classitem-1 activea hreflink4.htmlfourth item/a/lili classitem-0a hreflink5.htmlfifth item/a/li不过最常用的还是 items 方法
网页请求
PyQuery 本身还有网页请求功能而且会把请求下来的网页代码转为 PyQuery 对象。
from pyquery import PyQuery as pq
print pq(http://cuiqingcai.com/, headers{user-agent: pyquery})
print pq(http://httpbin.org/post, {foo: bar}, methodpost, verifyTrue)感受一下GETPOST样样通。
Ajax
PyQuery 同样支持 Ajax 操作带有 get 和 post 方法不过不常用一般我们不会用 PyQuery 来做网络请求仅仅是用来解析。 PyQueryAjax
API
API 原汁原味最全的 API都在里面了如果你对 jQuery 语法不熟强烈建议先学习下 jQuery再回来看 PyQuery你会感到异常亲切