这篇文章主要介绍爬虫的 pyquery模块

初始化

1.字符串初始化

from pyquery import PyQuery as pq
html = '''
<div class="wrap">
	<div id="container">
		<ul class="list">
			<li class="item-0">first item</li>
			<li class="item-1"><a rel="nofollow" href="link2.html">second item</a></li>
			<li class="item-0 active"><a rel="nofollow" href="link3.html"><span class="bold">third item</span></a></li>
			<li class="item-1 active"><a rel="nofollow" href="link4.html">fourth item</a></li>
			<li class="item-0"><a rel="nofollow" href="link5.html">fifth item</a></li>
		</ul>
	</div>
</div>
'''
doc = pq(html)   #初始化html
print(doc)       #打印
print(type(doc))  #打印类型

2.网页初始化

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com',encoding='utf-8')    #如果不加上encoding可能会出现乱码的情况。
print(doc)

这里插入一下requests访问百度的时候也会乱码,处理方式如下:

import requests 
html = requests.get('http://www.baidu.com').content  #先转为byte
html = str(html,'utf-8')    #通过str转为utf8的格式
print(html)

文件初始化

from pyquery import PyQuery as pq
doc = pq(filename='index.html')   #注意路径
print(doc)

CSS选择器

CSS3的选择参考手:http://www.w3school.com.cn/cssref/css_selectors.asp

from pyquery import PyQuery as pq
html = '''
<div id="container_1">
		<ul class="list">
				 <li class="item-0">first item</li>
				 <li class="item-1"><a rel="nofollow" href="link2.html">second item</a></li>
				 <li class="item-0 active"><a rel="nofollow" href="link3.html"><span class="bold">third item</span></a></li>
				 <li class="item-1 active"><a rel="nofollow" href="link4.html">fourth item</a></li>
				 <li class="item-0"><a rel="nofollow" href="link5.html">fifth item</a></li>
		 </ul>
 </div>
'''
doc = pq(html)
print(doc('li'))        #打印所有的li标签
print(type(doc('li')))
print(doc.find('li'))    #查考所有的li标签,和上面过滤内容一样
print(type(doc.find('li')))

输出结果为:

	<li class="item-0">first item</li>
     <li class="item-1"><a rel="nofollow" href="link2.html">second item</a></li>
     <li class="item-0 active"><a rel="nofollow" href="link3.html"><span class="bold">third item</span></a></li>
     <li class="item-1 active"><a rel="nofollow" href="link4.html">fourth item</a></li>
     <li class="item-0"><a rel="nofollow" href="link5.html">fifth item</a></li>
<class 'pyquery.pyquery.PyQuery'>
<li class="item-0">first item</li>
     <li class="item-1"><a rel="nofollow" href="link2.html">second item</a></li>
     <li class="item-0 active"><a rel="nofollow" href="link3.html"><span class="bold">third item</span></a></li>
     <li class="item-1 active"><a rel="nofollow" href="link4.html">fourth item</a></li>
     <li class="item-0"><a rel="nofollow" href="link5.html">fifth item</a></li>
<class 'pyquery.pyquery.PyQuery'>

打印class为list的标签:

print(doc('.list'))  

打印id为container的标签

print(doc('.list'))

打印li的父标签

item = doc('li')
print(item.parent())

打印祖先标签

print(item.parents())

打印 li 标签的祖先标签并且class="wrap"的祖先标签

item.parents('.wrap')  

获取兄弟标签

li = doc('.list .item-0.active')  
li.sibings()  

遍历

li = doc('.item-0.active')   单个元素
li = doc('li').items    通过items产生一个generator类型,使用for循环遍历

获取信息

·获取属性
a = doc('.item-0.active a')
a.attr('href')   #获取属性	
a.attr.href

·获取文本
a = doc('.item-0.active a')
a.text()   #获取标签内的内容

·获取html
li = doc('.item-0.active')
li.html()

DOM操作

·add_class、remove_class
	li = doc('.item-0.active')
	li.remove_class('active')     #删除active的属性
	li.add_class('active')    #添加class的属性

·attr、css
	li = doc('.item-0.active')
	li.attr('name','link')    #添加属性
	li.css('front-size','14px')     #添加CSS样式
	
·remove
	wrap = doc('.wrap')
	wrap.find('li').remove 删除找到的li   #只删除查找到的标签
	print(wrap)   

输出

	<div class="wrap">
<div id="container_1">
    <ul class="list">
         
         
         
         
         
    </ul>
</div>
</div>

#伪类选择器

li = doc('li:first-child')  #打印出查找的第一个li标签
print(li)

li = doc('li:last-child')   #打印出查找的第后一个li标签
print(li)

li = doc('li:nth-child(2)')  #获取第二个
print(li)

li = doc('li:gt(2)')   #获取第二个之后的
print(li)

li = doc('li:nth-child(2n)')   #获取偶数,2n+1 奇数

li = doc('li:contains(second)')  #获取li标签的内容中包含有second的标签