Python--python爬虫神器PyQuery
来源:互联网 发布:海康威视远程配置域名 编辑:程序博客网 时间:2024/06/05 10:01
安装
pip install pyquery
或者直接在PyCharm中import pyquery 提示没有的时候点击install就ok啦
初始化
在这里介绍四种初始化方式。
(1)直接字符串
from pyquery import PyQuery as pqdoc = pq("<html></html>")
pq 参数可以直接传入 HTML 代码,doc 现在就相当于 jQuery 里面的 $ 符号了。
(2)lxml.etree
from lxml import etreedoc = pq(etree.fromstring("<html></html>"))
可以首先用 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 的文件,文件内容为
<div> <ul> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div>
编写如下程序
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()
运行结果
<ul> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul><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 语法)的对象!
属性操作
你可以完全按照 jQuery 的语法来进行 PyQuery 的操作。
from pyquery import PyQuery as pqp = pq('<p id="hello" class="hello"></p>')('p')print p.attr("id")print p.attr("id", "plop")print p.attr("id", "hello")
运行结果
hello
<p id="plop" class="hello"/><p id="hello" class="hello"/>
再看一个例子
from pyquery import PyQuery as pqp = 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'})
运行结果
<p id="hello" class="hello beauty"/><p id="hello" class="beauty"/><p id="hello" class="beauty" style="font-size: 16px"/><p id="hello" class="beauty" style="font-size: 16px; background-color: yellow"/>
依旧是那么优雅与自信!
在这里我们发现了,这是一连串的操作,而 p 是一直在原来的结果上变化的。
因此执行上述操作之后,p 本身也发生了变化。
DOM操作
同样的原汁原味的 jQuery 语法
from pyquery import PyQuery as pqp = 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
运行结果
<p id="hello" class="hello"> check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><div class="wrap"><div id="test"><p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><a href="http://cuiqingcai.com">Germy</a></div></div><div 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<a href="link2.html">second item</a><a href="link3.html"><span class="bold">third item</span></a><a href="link4.html">fourth item</a><a href="link5.html">fifth item</a><li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li>
不过最常用的还是 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,样样通。
API
Ajax
- Python--python爬虫神器PyQuery
- python爬虫神器PyQuery的使用方法
- python爬虫神器PyQuery的使用方法
- python爬虫神器PyQuery的使用方法
- Python爬虫神器PyQuery的使用方法
- Python分析网页神器pyquery
- 【Python】网络爬虫(一):pyquery一瞥
- Python爬虫利器之PyQuery的用法
- python pyquery
- 基于Python、PyQuery实现的一个网络爬虫实例
- Python爬虫利器六之PyQuery的用法
- python爬虫由浅入深10---pyquery库的基础与使用
- python之pyquery 学习
- python安装pyquery失败
- Python PyQuery的用法
- python爬虫之神器正则表达式
- python中的爬虫神器 XPath 介绍
- python网络爬虫系列教程——python中pyquery库应用全解
- 占坑
- 自定义圆形图片
- 手机开发实战55——EMS介绍1
- codeforces 676C Vasya and String(尺取法)
- 格力 大松KJFC230A空气净化器开机自动灯闪烁不工作是什么原因?
- Python--python爬虫神器PyQuery
- 二叉树的分层打印(二)
- Java多线程创建方式
- andriod eclipse 如何设置ndk-build也即NDK自动编译C/C++、JNI程序
- C++ primer 继承与动态绑定
- Caused by: android.os.TransactionTooLargeException总结
- 用CocoaPods做iOS程序的依赖管理
- 手机开发实战56——EMS介绍2
- 无线信号的RSS和CSI的理解