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 ...
随机推荐
- docker nginx 反向代理
上一篇介绍了docker 安装和docker-compose配置 使用docker pull nginx:1.12.0 编写docker-compose.yml nginx: image: 17dae ...
- python编写脚本,删除固定用户下的所有表
脚本如下: [oracle@ycr python]$ more t_del.py #/usr/bin/python#coding:utf8 import sysimport cx_Oracle i=0 ...
- March 20 2017 Week 12 Monday
A goal is a dream with a deadline. 目标就是给梦想一个期限. Dream without dealine is just daydream, because you ...
- 动态规划(DP),压缩状态,插入字符构成回文字符串
题目链接:http://poj.org/problem?id=1159 解题报告: 1.LCS的状态转移方程为 if(str[i-1]==str[j-1]) dp[i][j]=dp[i-1][j-1] ...
- 【[AHOI2012]树屋阶梯】
卡特兰数! 至于为什么是卡特兰数,就稍微说那么一两句吧 对于一个高度为\(i\)的阶梯,我们可以在左上角填一个高度为\(k\)的阶梯,右下角填一个高度为\(i-1-k\)的阶梯剩下的我们用一个大的长方 ...
- POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】
题目传送门:http://poj.org/problem?id=3764 The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
- PHP设计模式——责任链模式
<?php /** * 责任链模式 * 组织一个对象链处理一个请求,每个处理对象知道自己能处理哪些请求,并把自己不能处理的请求交下一个处理对象 * * 适用场景: * 1.有多个对象可以处理同一 ...
- 如何为WebStorm设置SASS的File Watchers?
Webstorm是一个很牛叉的IDE,现在工作每天都是用它了. 最近开始用SASS,LESS等来写CSS,而在Webstorm中,它自带一个File Watchers功能,设置一下,即可实时编译SAS ...
- WPF学习笔记(7):DataGrid中数字自定义格式显示
DataGrid中数据显示如下图,数据格式比较杂乱.希望达到以下要求:(1)所有数据保留两位小数:(2)超过1000的数字显示千分位:(3)如果数据为0,不显示. 首先想到用StringFormat进 ...
- mysql安装下载
简单介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最 ...