Python 插件杂谈 (3) ---- PyQuery , Python中的网页分析利器
2010-08-15 07:56
387 查看
嗯哼,Meego中文核心站
-- 米趣网
又发新博文啦, PyQuery
是Python
下用于 分析网页内容
的工具。 有点像 BeautifulSoup
,但是功能更近似于 JQuery
,相信用过 BeautifulSoup
的朋友对他的操作深有印象,远不及 JQuery的语法来得简单明了。
先来段介绍:
PyQuery
允许你使用 JQuery 的语法访问 XML 文档。PyQuery的API尽可能地与JQuery相似。PyQuery
使用 lxml 快速分析 xml 和 html文档。
但是 PyQuery
不是(起码不再是)用来生成javascript 或者与javascript代码的库。我(作者本人)只是喜欢 JQuery 的 api 而在Python中找不到这样的工具,所以我告诉我自己在Python中制造这样的一个工具,于是就有了PyQuery
的产生。
PyQuery
可以用来实现多种用途,有一个主意我未来可能实现的,那就是使用它来模板化纯粹的http模板。我也用它进行网页抓取进而实现 装饰Deliverance 应用程序。
接着来段Demo,让大家对 PyQuery
有个认识:
>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> import urllib
>>> d = pq("<html></html>")
>>> d = pq(etree.fromstring("<html></html>"))
>>> d = pq(url='http://google.com/')
>>> d = pq(url='http://google.com/', opener=lambda url: urllib.urlopen(url).read())
>>> d = pq(filename=path_to_html_file)
复制代码
上面的 d 有点像 jQuery 中的 $
>>> d("#hello")
[<p#hello.hello>]
>>> p = d("#hello")
>>> p.html()
'Hello world !'
>>> p.html("you know <a href='http://python.org/'>Python</a> rocks")
[<p#hello.hello>]
>>> p.html()
u'you know <a href="http://python.org/">Python</a> rocks'
>>> p.text()
'you know Python rocks'
复制代码
你可以使用很多 JQuery 类似的语法, 不过不包括那样不在css标准中的语法,如::first :last :even
dd :eq :lt :gt :checked :selected :file:
>>> d('p:first')
[<p#hello.hello>]
复制代码
最后,我就不啰嗦了,为大家提供一下, PyQuery的文档
-- 米趣网
又发新博文啦, PyQuery
是Python
下用于 分析网页内容
的工具。 有点像 BeautifulSoup
,但是功能更近似于 JQuery
,相信用过 BeautifulSoup
的朋友对他的操作深有印象,远不及 JQuery的语法来得简单明了。
先来段介绍:
PyQuery
允许你使用 JQuery 的语法访问 XML 文档。PyQuery的API尽可能地与JQuery相似。PyQuery
使用 lxml 快速分析 xml 和 html文档。
但是 PyQuery
不是(起码不再是)用来生成javascript 或者与javascript代码的库。我(作者本人)只是喜欢 JQuery 的 api 而在Python中找不到这样的工具,所以我告诉我自己在Python中制造这样的一个工具,于是就有了PyQuery
的产生。
PyQuery
可以用来实现多种用途,有一个主意我未来可能实现的,那就是使用它来模板化纯粹的http模板。我也用它进行网页抓取进而实现 装饰Deliverance 应用程序。
接着来段Demo,让大家对 PyQuery
有个认识:
>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> import urllib
>>> d = pq("<html></html>")
>>> d = pq(etree.fromstring("<html></html>"))
>>> d = pq(url='http://google.com/')
>>> d = pq(url='http://google.com/', opener=lambda url: urllib.urlopen(url).read())
>>> d = pq(filename=path_to_html_file)
复制代码
上面的 d 有点像 jQuery 中的 $
>>> d("#hello")
[<p#hello.hello>]
>>> p = d("#hello")
>>> p.html()
'Hello world !'
>>> p.html("you know <a href='http://python.org/'>Python</a> rocks")
[<p#hello.hello>]
>>> p.html()
u'you know <a href="http://python.org/">Python</a> rocks'
>>> p.text()
'you know Python rocks'
复制代码
你可以使用很多 JQuery 类似的语法, 不过不包括那样不在css标准中的语法,如::first :last :even
dd :eq :lt :gt :checked :selected :file:
>>> d('p:first')
[<p#hello.hello>]
复制代码
最后,我就不啰嗦了,为大家提供一下, PyQuery的文档
相关文章推荐
- Python 插件杂谈 (4) ---- BeautifulSoup , Python中的网页分析工具
- 采用python的pyquery引擎做网页爬虫,进行数据分析
- Python分析网页神器pyquery
- Python 插件杂谈 (1) ---- chardet
- python分析网页上所有超链接的方法
- phpQuery-分析网页文本的新利器
- Speed Tracer由google开发的一款测试网页性能分析插件
- xpath: Python网页爬虫定位辅助利器
- 基于Python开发chrome插件的方法分析
- 浏览器中的开发人员工具(IE9的F12和Chrome的Ctrl+Shift+I)-网页分析的利器
- python使用htmllib分析网页内容的方法
- Python网页解析利器BeautifulSoup安装使用介绍
- phpQuery-分析网页文本的新利器(DOM处理利器)
- python使用正则表达式分析网页中的图片并进行替换的方法
- 网页解析利器goose的python3版本安装与测试
- Python 插件杂谈 (1) ---- chardet
- python beautifulsoup多线程分析抓取网页
- python下用HTMLParser分析网页方法
- python网页解析利器——BeautifulSoup
- Python获取网页内容、使用BeautifulSoup库分析html