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. Selenium WebDriver-判断页面中某一元素是否可操作

    driver.get("http://127.0.0.1/test_enable.html") i1=driver.find_element_by_id("input1& ...

  2. selenium2.53用45以下的火狐别太高

    selenium2.53用45以下的火狐别太高在高的火狐需要selenium3

  3. Django创建

    Pycharm里面Django模块安装及项目创建和启动: Pycharm里面Django模块安装(也可以指定安装源): 创建Django项目: 注意切换到合适的目录进行安装 diango-admin ...

  4. [git 学习篇] --创建git创库

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...

  5. maven项目打包jar,含有依赖jar

    在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...

  6. 【转】C#学习之用迭代器实现枚举器

    http://www.cnblogs.com/zouzf/archive/2012/02/22/2362954.html 本人初学C#,本文仅供个人整理思路用,那里说得不对,请大家多多指教,万分感激! ...

  7. 一句话学Java——Java重载和重写

    概念:重载是指两个不同的函数有相同的名称,可以是在本类之中的函数之间的重载,也可以是子类和父类的函数之间的函数重载. 重写:只能是子类重写父类的函数.这是多态的基础. 重写的规则:     参数:重写 ...

  8. 【bzoj2081】[Poi2010]Beads Hash

    题目描述 Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子串),每块有k(k>0)个珠子,如果这条珠子的长度不是k的倍数, ...

  9. bzoj 1061~1065【Noi2008】解题报告

    这次Noi好像格外喜欢树形DpQAQ P.S.好像这次的题都与图有关QAQ bzoj1061[Noi2008]志愿者招募:上下界可行最小费用流 bzoj1062[Noi2008]糖果雨:数形结合&am ...

  10. Java数据库连接JDBC用到哪种设计模式?

    还没看桥接模式,占tag 桥接模式: 定义 :将抽象部分与它的实现部分分离,使它们都可以独立地变化. 意图 :将抽象与实现解耦. 桥接模式所涉及的角色 1.  Abstraction :定义抽象接口, ...