Python爬虫利器六之PyQuery的用法
2017-05-17 10:23
447 查看
PyQuery是
Python仿照
jQuery的严格实现。语法与
jQuery几乎完全相同,所以不用再去费心去记一些奇怪的方法了。
天下竟然有这等好事?我都等不及了!
安装
有这等神器还不赶紧安装了!来!1 | pip install pyquery |
参考来源
本文内容参考官方文档,更多内容,大家可以去官方文档学习,毕竟那里才是最原汁原味的。目前版本 1.2.4 (2016/3/24)官方文档简介
pyquery allows you to make jquery queries on xml documents. The API isas much as possible the similar to jquery. pyquery uses lxml for fast
xml and html manipulation. This is not (or at least not yet) a library
to produce or interact with JavaScript code. I just liked the jquery
API and I missed it in python so I told myself “Hey let’s make jquery
in python”. This is the result. It can be used for many purposes, one
idea that I might try in the future is to use it for templating with
pure http templates that you modify using pyquery. I can also be used
for web scrapping or for theming applications with Deliverance.pyquery 可让你用 jQuery 的语法来对 xml 进行操作。这I和 jQuery 十分类似。如果利用 lxml,pyquery 对 xml 和 html 的处理将更快。这个库不是(至少还不是)一个可以和 JavaScript交互的代码库,它只是非常像 jQuery API 而已。
初始化
在这里介绍四种初始化方式。(1)直接字符串
Python1 2 | frompyqueryimportPyQueryaspq doc=pq("<html></html>") |
(2)lxml.etree
Python12 | from lxml import etreedoc = pq(etree.fromstring("<html></html>")) |
(3)直接传URL
1 2 | frompyqueryimportPyQueryaspq doc=pq('http://www.baidu.com') |
(4)传文件
12 | from pyquery import PyQuery as pqdoc = pq(filename='hello.html') |
快速体验
现在我们以本地文件为例,传入一个名字为 hello.html 的文件,文件内容为XHTML1 2 3 4 5 6 7 8 9 | <div> <ul> <liclass="item-0">first item</li> <liclass="item-1"><ahref="link2.html">second item</a></li> <liclass="item-0 active"><ahref="link3.html"><spanclass="bold">third item</span></a></li> <liclass="item-1 active"><ahref="link4.html">fourth item</a></li> <liclass="item-0"><ahref="link5.html">fifth item</a></li> </ul> </div> |
1234567 | from pyquery import PyQuery as pqdoc = pq(filename='hello.html')print doc.html()print type(doc)li = doc('li')print type(li)print li.text() |
1 2 3 4 5 6 7 8 9 10 11 | <ul> <liclass="item-0">firstitem</li> <liclass="item-1"><ahref="link2.html">seconditem</a></li> <liclass="item-0 active"><ahref="link3.html"><spanclass="bold">thirditem</span></a></li> <liclass="item-1 active"><ahref="link4.html">fourthitem</a></li> <liclass="item-0"><ahref="link5.html">fifthitem</a></li> </ul> <class'pyquery.pyquery.PyQuery'> <class'pyquery.pyquery.PyQuery'> firstitemseconditemthirditemfourthitemfifthitem |
在这里我们注意到了一点,PyQuery 初始化之后,返回类型是 PyQuery,利用了选择器筛选一次之后,返回结果的类型依然还是 PyQuery,这简直和 jQuery 如出一辙,不能更赞!然而想一下 BeautifulSoup 和 XPath 返回的是什么?列表!一种不能再进行二次筛选(在这里指依然利用 BeautifulSoup 或者 XPath 语法)的对象!
然而比比 PyQuery,哦我简直太爱它了!
属性操作
你可以完全按照 jQuery 的语法来进行 PyQuery 的操作。Python
123456 | from pyquery import PyQuery as pq p = pq('<p id="hello" class="hello"></p>')('p')print p.attr("id")print p.attr("id", "plop")print p.attr("id", "hello") |
1 2 3 | hello <pid="plop"class="hello"/> <pid="hello"class="hello"/> |
Python
1234567 | from pyquery import PyQuery as pq p = pq('<p id="hello" class="hello"></p>')('p')print p.addClass('beauty')print p.removeClass('hello')print p.css('font-size', '16px')print p.css({'background-color': 'yellow'}) |
1 2 3 4 | <pid="hello"class="hello beauty"/> <pid="hello"class="beauty"/> <pid="hello"class="beauty"style="font-size: 16px"/> <pid="hello"class="beauty"style="font-size: 16px; background-color: yellow"/> |
在这里我们发现了,这是一连串的操作,而 p 是一直在原来的结果上变化的。
因此执行上述操作之后,p 本身也发生了变化。
DOM操作
同样的原汁原味的 jQuery 语法Python
1234567891011 | from pyquery import PyQuery as pq p = pq('<p id="hello" class="hello"></p>')('p')print p.append(' check out <a href="http://reddit.com/r/python"><span>reddit</span></a>')print p.prepend('Oh yes!')d = pq('<div class="wrap"><div id="test"><a href="http://cuiqingcai.com">Germy</a></div></div>')p.prependTo(d('#test'))print pprint dd.empty()print d |
1 2 3 4 5 | <pid="hello"class="hello"> check out <ahref="http://reddit.com/r/python"><span>reddit</span></a></p> <pid="hello"class="hello">Oh yes! check out <ahref="http://reddit.com/r/python"><span>reddit</span></a></p> <pid="hello"class="hello">Oh yes! check out <ahref="http://reddit.com/r/python"><span>reddit</span></a></p> <divclass="wrap"><divid="test"><pid="hello"class="hello">Oh yes! check out <ahref="http://reddit.com/r/python"><span>reddit</span></a></p><ahref="http://cuiqingcai.com">Germy</a></div></div> <divclass="wrap"/> |
DOM 操作也是与 jQuery 如出一辙。
遍历
遍历用到 items 方法返回对象列表,或者用 lambda1234567 | from pyquery import PyQuery as pqdoc = pq(filename='hello.html')lis = doc('li')for li in lis.items(): print li.html() print lis.each(lambda e: e) |
1 2 3 4 5 6 7 8 9 10 | firstitem <ahref="link2.html">second item</a> <ahref="link3.html"><spanclass="bold">third item</span></a> <ahref="link4.html">fourth item</a> <ahref="link5.html">fifth item</a> <liclass="item-0">first item</li> <liclass="item-1"><ahref="link2.html">second item</a></li> <liclass="item-0 active"><ahref="link3.html"><spanclass="bold">third item</span></a></li> <liclass="item-1 active"><ahref="link4.html">fourth item</a></li> <liclass="item-0"><ahref="link5.html">fifth item</a></li> |
网页请求
PyQuery 本身还有网页请求功能,而且会把请求下来的网页代码转为 PyQuery 对象。1 2 3 | frompyqueryimportPyQueryaspq printpq('http://cuiqingcai.com/',headers={'user-agent':'pyquery'}) printpq('http://httpbin.org/post',{'foo':'bar'},method='post',verify=True) |
Ajax
PyQuery 同样支持 Ajax 操作,带有 get 和 post 方法,不过不常用,一般我们不会用 PyQuery 来做网络请求,仅仅是用来解析。PyQueryAjax
API
最后少不了的,API大放送。API
原汁原味最全的API,都在里面了!如果你对 jQuery 语法不熟,强烈建议先学习下 jQuery,再回来看 PyQuery,你会感到异常亲切!
结语
用完了 PyQuery,我已经深深爱上了他!你呢?
相关文章推荐
- 芝麻HTTP: Python爬虫利器之PyQuery的用法
- Python爬虫利器六之PyQuery的用法
- Python爬虫利器之PyQuery的用法
- Python爬虫利器一之Requests库的用法
- Python爬虫利器二之Beautiful Soup的用法
- Python爬虫利器一之Requests库的用法
- Python爬虫利器Beautiful Soup的用法
- Python爬虫利器二之Beautiful Soup的用法
- Python爬虫利器二之Beautiful Soup的用法
- Python爬虫利器五之Selenium的用法
- Python爬虫利器二之Beautiful Soup的用法
- Python爬虫辅助利器PyQuery模块的安装使用攻略
- Python爬虫利器之PhantomJS的用法
- Python爬虫利器三之Xpath语法与lxml库的用法
- Python爬虫利器二之Beautiful Soup的用法
- Python爬虫利器五之Selenium的用法
- Python爬虫利器之Selenium的用法
- Python爬虫利器五之Selenium的用法
- Python爬虫利器之Beautiful Soup的用法