Python3爬虫(七) 解析库的使用之pyquery
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的更多相关文章
- Python爬虫【解析库之beautifulsoup】
解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...
- Python爬虫【解析库之pyquery】
该库跟jQuery的使用方法基本一样 http://pyquery.readthedocs.io/ 官方文档 解析库的安装 pip3 install pyquery 初始化 1.字符串初始化 htm ...
- python爬虫三大解析库之XPath解析库通俗易懂详讲
目录 使用XPath解析库 @(这里写自定义目录标题) 使用XPath解析库 1.简介 XPath(全称XML Path Languang),即XML路径语言,是一种在XML文档中查找信息的语言. ...
- python爬虫之解析库Beautiful Soup
为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...
- 爬虫之解析库-----re、beautifulsoup、pyquery
一.介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- python3爬虫之Urllib库(二)
在上一篇文章中,我们大概讲了一下urllib库中最重要的两个请求方法:urlopen() 和 Request() 但是仅仅凭借那两个方法无法执行一些更高级的请求,如Cookies处理,代理设置等等 ...
- python3爬虫之Urllib库(一)
上一篇我简单说了说爬虫的原理,这一篇我们来讲讲python自带的请求库:urllib 在python2里边,用urllib库和urllib2库来实现请求的发送,但是在python3种在也不用那么麻烦了 ...
- python爬虫之解析库正则表达式
上次说到了requests库的获取,然而这只是开始,你获取了网页的源代码,但是这并不是我们的目的,我们的目的是解析链接里面的信息,比如各种属性 @href @class span 抑或是p节点里 ...
- python3爬虫之requests库基本使用
官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests 基于 urllib3 ,python编写. 安装 pip insta ...
随机推荐
- Laravel 教程 - 实战 iBrand 开源电商 API 系统
iBrand 简介 IYOYO 公司于2011年在上海创立.经过8年行业积累,IYOYO 坚信技术驱动商业革新,通过提供产品和服务助力中小企业向智能商业转型升级. 基于社交店商的核心价值,在2016年 ...
- Vim直接打开Tampermonkey网址的方法。
根据tampermonkey利用@require调用本地脚本的方法,比如我电脑上保存了Tampermonkey脚本a.user.js和它调用的a.js, 想在Vim编辑这两个文件时,都能一键打开网页里 ...
- 数据结构与算法分析java——树1
1. 基本术语 度(degree):一个节点的子树个数称为该节点的度: 树中结点度的最大值称为该树的度. 层数(level):从根结点开始算,根节点为1 高度(height)/深度(depth):节点 ...
- C++ decltype类型说明符(尾置返回类型使用)
转自https://blog.csdn.net/yhl_leo/article/details/50865552 1 基本语法 decltype 类型说明符生成指定表达式的类型.在此过程中,编译器分析 ...
- 【Oracle】三个配置文件tnsnames-listener-sqlnet介绍【转】
转自:博客园-oracle: listener.ora .sqlnet.ora .tnsnames.ora的配置及例子 介绍三个配置文件 1)listener.ora 2)sqlnet.ora 3)t ...
- java的引用总结
四种引用:强弱软虚 强引用:使用强引用,在内存不足的时候垃圾处理器也不会回收他,哪怕导致程序崩溃 例如: A a=new A() 软引用:内存不足的时候会被回收(软引用可以和一个引用队列(Refere ...
- jQuery中没有innerHtml和innerText
发现如果我在div或者其他非表单的标签中赋值,原本用普通的js就直接document.getElementById("id").innerHtml(或者其他几个)就可以了. 但是在 ...
- block与inline,inline和inline-block,块级和行内元素,行内替换和行内非替换元素
block:块级元素默认display属性为block:无论块内内容有多少,总是占满一行: inline:行内元素默认display属性为inline:只占据块内的内容的大小,不会占满一整行: inl ...
- Restframework中的Request
1.介绍 该篇博客主要介绍restframework内置的Request类,它扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等. 如: 在APIView中封装的r ...
- Ajax全接触(2)
例子简介 1.查询员工信息,可以通过输入员工编号查询员工基本信息: 2.新建员工信息,包含员工姓名,员工编号,员工性别,员工职位: 实现: 1.纯html页面,用来实现员工查询和新建的页面: 2.ph ...