pyquery库的使用
pyquery标签选择
获取了所有的img标签(css选择器,你也可以换成不同的class和id)
import requests
import re
from pyquery import PyQuery as pq
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Upgrade-Insecure-Requests": "",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
response=requests.get('https://www.zhihu.com/question/35239964/answer/66644148',headers=headers,timeout=9)
doc=pq(response.content)
#css选择器
a=doc('img')#<class 'pyquery.pyquery.PyQuery'>
print(a)
url初始化(通过访问url得到html代码)
有了pyquery,你甚至不需要再使用requests来get网页
from pyquery import PyQuery as pq
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Upgrade-Insecure-Requests": "",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
doc=pq(url='https://www.zhihu.com/question/35239964/answer/66644148',headers=headers)#直接初始化url得到源代码
print(doc('title').text())
文件初始化(通过文件得到html代码)
#文件初始化
from pyquery import PyQuery as pq
import requests
# 写文件
# headers={
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
# "Accept-Encoding": "gzip, deflate",
# "Accept-Language": "zh-CN,zh;q=0.9",
# "Upgrade-Insecure-Requests": "1",
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
# }
# result=requests.get('https://www.zhihu.com/question/35239964/answer/66644148',headers=headers)
# # result=pq(url='https://www.zhihu.com/question/35239964/answer/66644148',headers=headers)
# print(type(result))
# with open("zhihu.html",'wb')as f:
# f.write(result.content)
#读文件
doc=pq(filename='test.html')
print(doc('title'))
css选择器
doc('#content .list li')#得到的是所有的符合这种层级关系的li
查找元素
子元素
find(查内部的元素)
html=‘.....’
doc=py(html)
a=doc('.content')
b=a.find('img')查找.content内的img标签
children(和find一样)
html=‘.....’
doc=py(html)
a=doc('.content')
b=a.children('img')查找.content内的img标签
父元素
html=‘.....’
doc=py(html)
a=doc('.content')
b=a.parent()查找.content的父元素整体
html=‘.....’
doc=py(html)
a=doc('.content')
b=a.parents()遍历输出.content的所有祖先元素整体
当然也可以加上css选择器
html=‘.....’
doc=py(html)
a=doc('.content')
b=a.parents(‘.wrap’)查找.content的祖先节点中为.wrap的标签
兄弟元素
html=‘.....’
doc=py(html)
a=doc('.content')
b=a.siblings()#查找.content的同级标签,也可以加css选择器
遍历查找的元素
from pyquery import PyQuery as pq
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Upgrade-Insecure-Requests": "",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
doc=pq(url='https://www.zhihu.com/question/35239964/answer/66644148',headers=headers)
a=doc('img').items()#得到可迭代对象
for i in a:
print(i)
<img src="data:image/svg+xml;utf8,<svg%20xmlns='http://www.w3.org/2000/svg'%20width='503'%20height='410'></svg>" data-rawwidth="503" data-rawheight="410" class="origin_image zh-lightbox-thumb lazy" width="503" data-original="https://pic4.zhimg.com/d8d9743a16e6db3f36b19a3397469b1d_r.jpg" data-actualsrc="https://pic4.zhimg.com/50/d8d9743a16e6db3f36b19a3397469b1d_hd.jpg"/>
加入i是得到的上述的html元素
获取属性:jpgurl=i.attr('data-original')
获取文本:text=i.text()
获取HTML:html=i.html()//获取i里面的html元素
DOM操作
addclass(添加class)、removeclass(移除class)
attr: .attr('name':'userid')//添加或替换name属性
css: .css('height':'500px')//添加或替换style
remove:
http='''
<div class='wrap'>
helloworld
<p>this is p</p>
</div>'''
from pyquery import PyQuery as pq
doc=pq(http)
wrap=doc('.wrap')
print(wrap.text())#helloworld this is p
wrap.find('p').remove()
print(wrap.text())#helloworld
pyquery库的使用的更多相关文章
- python爬虫从入门到放弃(七)之 PyQuery库的使用
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...
- 爬虫常用库之pyquery 库
pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的.他的官方文档地址是:http://packages. ...
- Python中PyQuery库的使用总结
介绍 pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,官方文档地址是:http://packages.python.org/pyquery/ pyquery 可让你用 ...
- Python爬虫-- PyQuery库
PyQuery库 PyQuery库也是一个非常强大又灵活的网页解析库,PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪 ...
- PYTHON 爬虫笔记六:PyQuery库基础用法
知识点一:PyQuery库详解及其基本使用 初始化 字符串初始化 html = ''' <div> <ul> <li class="item-0"&g ...
- 学习PyQuery库
学习PyQuery库 好了,又是学习的时光啦,今天学习pyquery 来进行网页解析 常规导入模块(PyQuery库中的pyquery类) from pyquery import PyQuery as ...
- python之爬虫(九)PyQuery库的使用
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...
- Python中PyQuery库的使用
pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的. 它的官方文档地址是:http://packages ...
- PyQuery库
'''强大又灵活的网页解析库.如果你觉得正则写起来太麻烦,又觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的绝佳选择.'''from pyquery ...
- 爬虫6:pyquery库
强大又灵活的网页解析库,如果觉得正则写起来太麻烦,BeautifulSoup语法太难记,而你又熟悉jQuery的语法,那么用PyQuery就是最佳选择 一. 初始化 1. 字符串初始化 h ...
随机推荐
- day03_10 注释及简单的用户输入输出
单行注释# print ("我爱北京天安门") print ("我爱北京天安门") #print ("我爱北京天安门") #print (& ...
- [python][oldboy]字符串 format
#coding=utf8 def format(self, *args, **kwargs): # known special case of str.format """ ...
- MySQL可供选择的存储引擎
备注:以下关于5.7版本的内容是来源于官方文档:https://dev.mysql.com/doc/refman/5.7/en/storage-engines.html 以下关于5.6版本的内容,一部 ...
- POJ 1656 Counting Black
Counting Black Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9772 Accepted: 6307 De ...
- 【Luogu】P2155沙拉公主的困惑(数论)
题目链接 数论果然是硬伤qwq 还是智商上的硬伤 我们来讲两个道理 No.1 求1~i!中与i!互质的数的个数 实际上就是求i!的欧拉函数 有如下递推式: f[1]=1 if(i为合数) f[i]=f ...
- fish shell安装和配置
sudo apt-get install fish whereis fish chsh -s /usr/bin/fish 重启:
- P1382 楼房 (扫描线,线段树)
题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平线高度为0.在轮廓 ...
- php错误报告
; This directive controls whether or not and where PHP will output errors, ; notices and warnings to ...
- 区间合并 POJ3667+HDU4553
两道题都是线段树的区间合并 lsum, rsum分别表示左/右端点 开始向右/左 符合条件的元素的最长连续长度 sum表示这个区间的符合条件的元素的最长连续长度 所以pushUp可写: void pu ...
- Mysql之Handler_read%
纯属自己理解,如有误导概不负责O(∩_∩)O 加索引: mysql> flush status; Query OK, rows affected (0.00 sec) mysql> flu ...