Infi-chu:

http://www.cnblogs.com/Infi-chu/

pyquery专门针对CSS和jQuery的操作处理

1.初始化
字符串初始化

from pyquery import PyQuery as pq
doc = pq(html) # 传入html文本
print(doc('li'))

URL初始化

from pyquery import PyQuery as pq
doc = pq(url='www.baidu.com')
print(doc('title'))
# 另一种方法
from pyquery import PyQuery as pq
import requests
doc = pq(requests.get('http://www.baidu.com'))
print(doc('title'))

文件初始化

from pyquery import PyQuery as pq
doc = pq(filename='text.html')
print(doc('li'))

2.基本CSS选择器

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
print(doc(#head .head_wrapper a))
print(type(doc(#head .head_wrapper a)))

3.查找节点
子节点

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
items = doc('.head_wrapper')
print(type(items))
print(items)
lis = items.find('a') # find()是查找符合条件的所有子孙节点,只查找子节点的可以使用children()
print(type(lis))
print(lis)

父节点
使用parent()方法获取该节点的父节点
使用parents()方法获取该节点的祖先节点

兄弟节点
使用siblings()方法获取兄弟节点

4.遍历

from pyquery import PyQuery as pq
doc = pq(html)
lis = doc('li').items()
print(type(lis))
for li in lis:
print(li,type(li))

5.获取信息
获取属性
使用attr()方法获取属性(值)

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
items = doc('.head_wrapper')
print(items.attr('href'))
# 也可以写成
print(items.attr.href) # 获取所有a的属性
from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
a = doc('a')
for i in a:
print(i.attr.href)

获取文本
使用text()方法获取纯文本纯字符串内容

from pyquery import PyQuery as pq
doc = pq(url = 'http://www.baidu.com')
a = doc('a')
print(i.text()) # 无需遍历

使用html()方法保留标签内部的东西

from pyquery import PyQuery as pq
doc = pq(url = 'http://www.baidu.com')
a = doc('a')
for i in a:
print(i)
print(i.html())

6.节点操作
addClass和removeClass

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 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 active"><a href="link5.html">fifth item</a></li>
</ul>
</div>
</div>
'''
doc = pq(html)
li = doc('.item-0 active')
print(li)
li.removeClass('active')
print(li)
li.addClass('active')
print(li)

attr、text和html

from pyquery import PyQuery as pq
html = '''
<div class="div">
<p>ASD</p>
<ul class="list">
<li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
</ul>
</div>
'''
doc = pq(html)
li = doc('.item-0 active')
print(li)
li.attr('name','link')
print(li)
li.text('changed item')
print(li)
li.html('<span>changed item</span>')
print(li)

remove()

from pyquery import PyQuery as pq
doc = pq(html)
res = doc('.div')
print(res.find('ul').remove().text())

7.伪类选择器
待完善

Python3爬虫(七) 解析库的使用之pyquery的更多相关文章

  1. Python爬虫【解析库之beautifulsoup】

    解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...

  2. Python爬虫【解析库之pyquery】

    该库跟jQuery的使用方法基本一样  http://pyquery.readthedocs.io/ 官方文档 解析库的安装 pip3 install pyquery 初始化 1.字符串初始化 htm ...

  3. python爬虫三大解析库之XPath解析库通俗易懂详讲

    目录 使用XPath解析库 @(这里写自定义目录标题) 使用XPath解析库 1.简介   XPath(全称XML Path Languang),即XML路径语言,是一种在XML文档中查找信息的语言. ...

  4. python爬虫之解析库Beautiful Soup

    为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...

  5. 爬虫之解析库-----re、beautifulsoup、pyquery

    一.介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...

  6. python3爬虫之Urllib库(二)

    在上一篇文章中,我们大概讲了一下urllib库中最重要的两个请求方法:urlopen()  和  Request() 但是仅仅凭借那两个方法无法执行一些更高级的请求,如Cookies处理,代理设置等等 ...

  7. python3爬虫之Urllib库(一)

    上一篇我简单说了说爬虫的原理,这一篇我们来讲讲python自带的请求库:urllib 在python2里边,用urllib库和urllib2库来实现请求的发送,但是在python3种在也不用那么麻烦了 ...

  8. python爬虫之解析库正则表达式

    上次说到了requests库的获取,然而这只是开始,你获取了网页的源代码,但是这并不是我们的目的,我们的目的是解析链接里面的信息,比如各种属性  @href  @class span  抑或是p节点里 ...

  9. python3爬虫之requests库基本使用

    官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests  基于  urllib3 ,python编写. 安装 pip insta ...

随机推荐

  1. x64 分页机制——虚拟地址到物理地址寻址

    原博客:http://www.cnblogs.com/lanrenxinxin/p/4735027.html 详细的理论讲解都在上面 下面说的是通过windbg手动进行寻址,深入理解 x64: 实践: ...

  2. 四、获取远程URL图片

    #!/usr/bin/python # -*- coding: UTF-8 -*- import re import urllib def getHtml(url): page = urllib.ur ...

  3. python入门2 python字符串换行显示、字符串太长\连接多行

    #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 缩进 换行 """ &qu ...

  4. 大小端,"字节序"

    2 字节序 2.1 字节 字节(Byte)作为计算机世界的计量单位,和大家手中的人民币多少多少“元”一个意思.反正,到了计算机的世界,说字节就对了,使用人家的基本计量单位,这是入乡随俗. 比如,一个电 ...

  5. Arcgis Javascript中geometryEngine报错’hq‘of undefined的解决方法

    这个问题困扰了我一个星期,原因是使用geomagicbuffer时候,有的线可正常使用,有的就直接报错,一直没有解决,后来发现是api自己的bug导致的 干脆直接读代码,在geometryEngine ...

  6. BZOJ4001:[TJOI2015]概率论(卡特兰数,概率期望)

    Description Input 输入一个正整数N,代表有根树的结点数 Output 输出这棵树期望的叶子节点数.要求误差小于1e-9 Sample Input 1 Sample Output 1. ...

  7. php 的 number_format使用

    $num = 1.0258963147; // 1.0259 $num = number_format($num, 4, '.', ''); $num = number_format($num); $ ...

  8. Unity 游戏框架搭建 (十三) 无需继承的单例的模板

    之前的文章中介绍的Unity 游戏框架搭建 (二) 单例的模板和Unity 游戏框架搭建 (三) MonoBehaviour单例的模板有一些问题. 存在的问题: 只要继承了单例的模板就无法再继承其他的 ...

  9. jquery mobile 移动web(3)

    可折叠功能块. div 元素的 data-role 属性设置为 collapsible 代码如下: <div data-role="collapsible"> < ...

  10. SAP物料主数据的屏幕字段控制,必输,隐藏

    http://www.cnblogs.com/275147378abc/p/5699077.html 1.事务码MM01,把物料组设为选填字段. 2.找到物料组的屏幕字段. 3.在后台根据屏幕字段找到 ...