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 ...
随机推荐
- The Tao to Excellent 2
就算自己现在的技术水平很菜,就算自己现在写的代码还是很烂,但我们还是要一直坚持在最前线,一直向上,也许,在前方,就有不一样的风景在等着我们. 因为我深深明白到一件事:人之所以选择混吃等死,是因为他们根 ...
- 【luogu P2023 [AHOI2009]维护序列】 题解
题目链接:https://www.luogu.org/problemnew/show/P2023 把P3373改一改直接粘过来就A #include <iostream> #include ...
- java 注解annotation的使用,以及反射如何获取注解
一.注解基本知识 1.元注解 元注解是指注解的注解.包括 @Retention @Target @Document @Inherited四种. 1. Annotation型定义为@interfac ...
- HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- kinect v2
http://www.tuicool.com/articles/NbmyyeU https://channel9.msdn.com/Blogs/raw-tech/Making-your-body-th ...
- 关于object类的两个重要方法以及为什么重写equals一定要重写hashcode()
toString()----------------------输出对象的地址 重写后输出对象的值对象.equals(对象)---------------比较两个对象的内存地址 可以被重写,重写后比较 ...
- ccenteros 部署 redis
step one : yum install redis -- 安装redis数据库 step two:安装完成之后开启redis 服务 service redis start syste ...
- ORA-12154/ORA-12560 可以尝试的解决办法
WIN10 本机安装ORACLE数据库和ORACLE客户端后,使用PL/SQL 登陆提示错误ORA-12154 和ORACLE-12560, 在检查了本机的注册表.环境PATH路径.tnsnames ...
- Java解决跨域问题
同源 URL由协议, 域名, 端口组成. 如果两个URL的协议, 域名, 端口相同, 那么这两个URL为同源. 同源策略 script, iframe, link 可以跨域加载一些静态资源, 比如脚本 ...
- LeetCode 简单 - 最大子序和(53)
采用动态规划方法O(n) 设sum[i]为以第i个元素结尾且和最大的连续子数组.假设对于元素i,所有以它前面的元素结尾的子数组的长度都已经求得,那么以第i个元素结尾且和最大的连续子数组实际上,要么是以 ...