Python爬虫神器:PyQuery 的使用方法,用了你会爱上它的

pyquery 可让你用 jQuery 的语法来对 xml 进行操作。这I和 jQuery 十分类似。如果利用 lxml,pyquery 对 xml 和 html 的处理将更快。

这个库不是(至少还不是)一个可以和 JavaScript交互的代码库,它只是非常像 jQuery API 而已。

初始化

在这里介绍四种初始化方式。

(1)直接字符串

from pyquery import PyQuery as pqdoc = pq("")


pq 参数可以直接传入 HTML 代码,doc 现在就相当于 jQuery 里面的 $ 符号了。

(2)lxml.etree

from lxml import etreedoc = pq(etree.fromstring(""))


可以首先用 lxml 的 etree 处理一下代码,这样如果你的 HTML 代码出现一些不完整或者疏漏,都会自动转化为完整清晰结构的 HTML代码。

(3)直接传URL

from pyquery import PyQuery as pqdoc = pq('http://www.baidu.com')


这里就像直接请求了一个网页一样,类似用 urllib2 来直接请求这个链接,得到 HTML 代码。

(4)传文件

from pyquery import PyQuery as pqdoc = pq(filename='hello.html')


可以直接传某个路径的文件名。

快速体验

现在我们以本地文件为例,传入一个名字为 hello.html 的文件,文件内容为

  • class="item-0">first item
  • class="item-1">
  • class="item-0 active">
  • class="item-1 active">
  • class="item-0">


编写如下程序

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()

运行结果

  • class="item-0">first item
  • class="item-1">
  • class="item-0 active">
  • class="item-1 active">
  • class="item-0">
<class
'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,哦我简直太爱它了!

属性操作

你可以完全按照 jQuery 的语法来进行 PyQuery 的操作。

from pyquery import PyQuery as pq p = pq('

')('p')print p.attr("id")print p.attr("id", "plop")print p.attr("id", "hello")

运行结果

hello

class="hello"/>

class="hello"/>

再来一发

from pyquery import PyQuery as pq p = pq('

')('p')print p.addClass('beauty')print p.removeClass('hello')print p.css('font-size', '16px')print p.css({'background-color': 'yellow'})

运行结果

class="hello beauty"/>

class="beauty"/>

class="beauty" style="font-size: 16px"/>

class="beauty" style="font-size: 16px; background-color: yellow"/>

依旧是那么优雅与自信!

在这里我们发现了,这是一连串的操作,而 p 是一直在原来的结果上变化的。

因此执行上述操作之后,p 本身也发生了变化。

DOM操作

同样的原汁原味的 jQuery 语法

from pyquery import PyQuery as pq p = pq('

')('p')print p.append(' check out ')print p.prepend('Oh yes!')d = pq('

')p.prependTo(d('#test'))print pprint dd.empty()print d

运行结果

id="hello" class="hello"> check out

id="hello" class="hello">Oh yes! check out

id="hello" class="hello">Oh yes! check out

class="wrap">
id="test">

id="hello" class="hello">Oh yes! check out

class="wrap"/>

这不需要多解释了吧。

DOM 操作也是与 jQuery 如出一辙。

遍历

遍历用到 items 方法返回对象列表,或者用 lambda

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)


运行结果

first item
  • class="item-0">first item
  • class="item-1">
  • class="item-0 active">
  • class="item-1 active">
  • class="item-0">
  • 不过最常用的还是 items 方法

    网页请求

    PyQuery 本身还有网页请求功能,而且会把请求下来的网页代码转为 PyQuery 对象。

    from pyquery import PyQuery as pqprint pq('http://cuiqingcai.com/', headers={'user-agent': 'pyquery'})print pq('http://httpbin.org/post', {'foo': 'bar'}, method='post', verify=True)

    感受一下,GET,POST,样样通。

    Ajax

    PyQuery 同样支持 Ajax 操作,带有 get 和 post 方法,不过不常用,一般我们不会用 PyQuery 来做网络请求,仅仅是用来解析。

    PyQueryAjax

    API

    最后少不了的,API大放送。

    API (https://pythonhosted.org/pyquery/api.html)

    原汁原味最全的API,都在里面了!如果你对 jQuery 语法不熟,强烈建议先学习下 jQuery,再回来看 PyQuery,你会感到异常亲切!

    结语

    用完了 PyQuery,我已经深深爱上了他!


    分享到:


    相關文章: