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,&lt;svg%20xmlns='http://www.w3.org/2000/svg'%20width='503'%20height='410'&gt;&lt;/svg&gt;" 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库的使用的更多相关文章

  1. python爬虫从入门到放弃(七)之 PyQuery库的使用

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  2. 爬虫常用库之pyquery 库

    pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的.他的官方文档地址是:http://packages. ...

  3. Python中PyQuery库的使用总结

    介绍 pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,官方文档地址是:http://packages.python.org/pyquery/ pyquery 可让你用 ...

  4. Python爬虫-- PyQuery库

    PyQuery库 PyQuery库也是一个非常强大又灵活的网页解析库,PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪 ...

  5. PYTHON 爬虫笔记六:PyQuery库基础用法

    知识点一:PyQuery库详解及其基本使用 初始化 字符串初始化 html = ''' <div> <ul> <li class="item-0"&g ...

  6. 学习PyQuery库

    学习PyQuery库 好了,又是学习的时光啦,今天学习pyquery 来进行网页解析 常规导入模块(PyQuery库中的pyquery类) from pyquery import PyQuery as ...

  7. python之爬虫(九)PyQuery库的使用

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  8. Python中PyQuery库的使用

    pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的. 它的官方文档地址是:http://packages ...

  9. PyQuery库

    '''强大又灵活的网页解析库.如果你觉得正则写起来太麻烦,又觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的绝佳选择.'''from pyquery ...

  10. 爬虫6:pyquery库

      强大又灵活的网页解析库,如果觉得正则写起来太麻烦,BeautifulSoup语法太难记,而你又熟悉jQuery的语法,那么用PyQuery就是最佳选择     一. 初始化 1. 字符串初始化 h ...

随机推荐

  1. 包含min函数的栈 【微软面试100题 第二题】

    题目要求:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(1). 参考题目:剑指offer第21题. 题目分析: 1. ...

  2. Clickonce - Change deployment URL after publish

    mage.exe -Update C:\inetpub\wwwroot\aspnet40\AminoScience\Uploads\Application Files\AccUFeed_1_0_0_5 ...

  3. JDBC 学习笔记(八)—— ResultSet

    JDBC 使用 ResultSet 来封装 SQL 的查询结果,可以将 ResultSet 类比为数据库表的查询结果. 它拥有如下两个性质: 可滚动. 可更新. 这两个性质,是在创建 Statemen ...

  4. kb-01-e<取余操作,宽搜,巧妙>;

    题目描述: n属于1到200,找到对应的一个数只含有0和1,并且是n的倍数: 分析: 本题有几个数会是大数:所以要考虑大数: 用到余数的性质:例如n=6,1%6=1: 1*10%6=4:       ...

  5. python hashlib模块 logging模块 subprocess模块

    一 hashlib模块 import hashlib md5=hashlib.md5() #可以传参,加盐处理 print(md5) md5.update(b'alex') #update参数必须是b ...

  6. UVA 116 Unidirectional TSP(DP最短路字典序)

    Description    Unidirectional TSP  Background Problems that require minimum paths through some domai ...

  7. scss 侧边栏_图片

    html <!doctype html><html lang="zh-CN"><head> <meta charset="UTF ...

  8. 一款多功能的移动端滚动选择器,支持单选到多选、支持多级级联、提供自定义回调函数、提供update函数二次渲染、重定位函数、兼容pc端拖拽等等..

    https://github.com/onlyhom/mobileSelect.js/blob/master/docs/README-CN.md mobileSelect.js 一款多功能的移动端滚动 ...

  9. Epoll详解及源码分析【转】

    转自:http://blog.csdn.net/chen19870707/article/details/42525887 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] ...

  10. py2exe打包整个项目

    这段时间做了用Python做了一个科学计算的项目,项目中用到了很多的第三方Python库,包括PyQt.traits.traitsui.matplotlib.pyface.table.numpy.tv ...