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. 转:获取GridView中RowCommand的当前索引行

    获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设 ...

  2. SDOJ 3740 Graph

    8.9 t3 [描述] 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点 到达. 2. ...

  3. Android线程中使用Toast、dialog、loading

    代码改变世界 Android线程中使用Toast.dialog.loading Loading: Thread t1 = new Thread(new Runnable() { @Override p ...

  4. SpriteKit-SKView

    1.暂停这个视图 @property (nonatomic, getter = isPaused) BOOL paused; 2.视图性能的一些参数 @property (nonatomic) BOO ...

  5. Luogu【P1725】琪露诺(单调队列,DP)

    本文是笔者第二篇解题报告.从现在开始,会将练的一些题发到博客上并归类到"解题报告"标签中. 琪露诺是这样一道题 这道题可以用纯DP做,但是据说会超时.(为什么?看起来过河这题比它数 ...

  6. kb-07线段树--10--dfs序建树

    /* hdu3974 dfs序建树,然后区间修改查询: */ #include<iostream> #include<cstdio> #include<cstring&g ...

  7. DataSet用法一:添加代码创建的表DataTable,设置主键外键,读取及修改DataSet表中数据

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  8. 36深入理解C指针之---结构体的内存处理

    一.有关结构体的内存处理包括,结构体指针和结构体成员指针的内存分配.结构体成员的数据对齐.结构体的内存释放 1.定义:与自定义数据类型(结构体)有关的内存分配.大小和释放问题 2.特征: 1).用内存 ...

  9. Perl语言入门--3--perl的控制结构

    表达式真假值总结: 表达式不一定是逻辑表达式,但一定要得出真假值   假值:逻辑值为假 值为0 字符串为空 列表为空 undef 其他情况为真 1.if {} elsif {} else {} 2.u ...

  10. UVA - 10196:Check The Check

    类型:简单模拟 大致题意:已知国际象棋行棋规则,给你一个局面,问是否将军?谁将谁的军?(保证不会同时将军) 思路:都以小写字母 测试 是否将 大写字母. 然后一个局面测两次(一次直接测,一次反转棋盘, ...