前言

你是否觉得 XPath 的用法多少有点晦涩难记呢?

你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢?

你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢?

你是否已经有了一些前端基础了解选择器却与另外一些奇怪的选择器语法混淆了呢?

嗯,那么,前端大大们的福音来了,PyQuery 来了,乍听名字,你一定联想到了 jQuery,如果你对 jQuery 熟悉,那么 PyQuery 来解析文档就是不二之选!包括我在内!

PyQueryPython 仿照 jQuery 的严格实现。语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪的方法了。

天下竟然有这等好事?我都等不及了!

安装

有这等神器还不赶紧安装了!来!

1 pip install pyquery

参考来源

本文内容参考官方文档,更多内容,大家可以去官方文档学习,毕竟那里才是最原汁原味的。

目前版本 1.2.4 (2016/3/24)

官方文档

简介

pyquery allows you to make jquery queries on xml documents. The API is
as 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)直接字符串

12 from pyquery import PyQuery as pqdoc = pq(\”\”)

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

(2)lxml.etree

12 from lxml import etreedoc = pq(etree.fromstring(\”\”))

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

(3)直接传URL

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

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

(4)传文件

12 from pyquery import PyQuery as pqdoc = pq(filename=\’hello.html\’)

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

快速体验

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

123456789
    

编写如下程序

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

运行结果

123 data-settings=\”show\”>
123؀些奇怪的选择器语法混淆了呢?

嗯,那么,前端大大们的福音来了,PyQuery 来了,乍听名字,你一定联想到了 jQuery,如果你对 jQuery 熟悉,那么 PyQuery 来解析文档就是不二之选!包括我在内!

PyQueryPython 仿照 jQuery 的严格实现。语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪的方法了。

天下竟然有这等好事?我都等不及了!

安装

有这等神器还不赶紧安装了!来!

1 pip install pyquery

参考来源

本文内容参考官方文档,更多内容,大家可以去官方文档学习,毕竟那里才是最原汁原味的。

目前版本 1.2.4 (2016/3/24)

官方文档

简介

pyquery allows you to make jquery queries on xml documents. The API is
as 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)直接字符串

12 from pyquery import PyQuery as pqdoc = pq(\”\”)

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

(2)lxml.etree

12 from lxml import etreedoc = pq(etree.fromstring(\”\”))

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

(3)直接传URL

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

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

(4)传文件

12 from pyquery import PyQuery as pqdoc = pq(filename=\’hello.html\’)

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

快速体验

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

123456789
    

编写如下程序

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

运行结果