首页 技术 正文
技术 2022年11月15日
0 收藏 599 点赞 4,980 浏览 2460 个字

0.参考

1.初始化

In [325]: from scrapy import SelectorIn [326]: text="""
...: <div>
...: <a>1a</a>
...: <p>2p</p>
...: <p>3p</p>
...: </div>"""In [327]: sel=Selector(text=text)In [328]: print(sel.extract())
<html><body><div>
<a>1a</a>
<p>2p</p>
<p>3p</p>
</div></body></html>

2.Xpath 父节点/上一个下一个兄弟节点

In [329]: sel.xpath('//a/parent::*/p').extract()
Out[329]: ['<p>2p</p>', '<p>3p</p>']In [330]: sel.xpath('//p/preceding-sibling::a').extract()
Out[330]: ['<a>1a</a>']In [331]: sel.xpath('//a/following-sibling::p').extract()
Out[331]: ['<p>2p</p>', '<p>3p</p>']

3.CSS 第几个子节点

3.1 通用

#完整子节点列表,从第一个子节点开始计数,并且满足子节点tag限定
In [332]: sel.css('a:nth-child(1)').extract()
Out[332]: ['<a>1a</a>']
#完整子节点列表,从最后一个子节点开始计数,并且满足子节点tag限定
In [333]: sel.css('a:nth-last-child(1)').extract()
Out[333]: []In [334]: sel.css('p:nth-child(1)').extract()
Out[334]: []In [335]: sel.css('p:nth-child(2)').extract()
Out[335]: ['<p>2p</p>']In [336]: sel.css('p:nth-child(3)').extract()
Out[336]: ['<p>3p</p>']In [337]: sel.css('p:nth-last-child(1)').extract()
Out[337]: ['<p>3p</p>']In [338]: sel.css('p:nth-last-child(2)').extract()
Out[338]: ['<p>2p</p>']In [339]: sel.css('p:nth-last-child(3)').extract()
Out[339]: []

3.2 特别指代

In [340]: sel.css('a:first-child').extract()
Out[340]: ['<a>1a</a>']In [341]: sel.css('a:last-child').extract()
Out[341]: []In [342]: sel.css('p:first-child').extract()
Out[342]: []In [343]: sel.css('p:last-child').extract()
Out[343]: ['<p>3p</p>']

3.3 上述 -child 修改为 -of-type ,仅对 过滤后的相应子节点列表 进行计数

4.Xpath 第几个子节点

In [344]: sel.xpath('//div').extract()
Out[344]: ['<div>\n <a>1a</a>\n <p>2p</p>\n <p>3p</p>\n</div>']In [345]: sel.xpath('//div/*').extract()
Out[345]: ['<a>1a</a>', '<p>2p</p>', '<p>3p</p>']In [346]: sel.xpath('//div/node()').extract()
Out[346]: ['\n ', '<a>1a</a>', '\n ', '<p>2p</p>', '\n ', '<p>3p</p>', '\n']In [347]: sel.xpath('//div/a').extract()
Out[347]: ['<a>1a</a>']In [348]: sel.xpath('//div/p').extract()
Out[348]: ['<p>2p</p>', '<p>3p</p>']In [349]:In [349]: sel.xpath('//div/a[1]').extract()
Out[349]: ['<a>1a</a>']In [350]: sel.xpath('//div/a[last()]').extract()
Out[350]: ['<a>1a</a>']In [351]:In [351]: sel.xpath('//div/p[1]').extract() #相当于过滤后的子节点列表
Out[351]: ['<p>2p</p>']In [352]: sel.xpath('//div/p[last()]').extract()
Out[352]: ['<p>3p</p>']In [353]: sel.xpath('//div/p[last()-1]').extract()
Out[353]: ['<p>2p</p>']In [354]:In [354]: sel.xpath('//div/*[1]').extract() #完整子节点列表
Out[354]: ['<a>1a</a>']In [355]: sel.xpath('//div/*[last()]').extract()
Out[355]: ['<p>3p</p>']In [356]:In [356]: sel.xpath('//div/node()[1]').extract() #包括纯文本
Out[356]: ['\n ']In [357]: sel.xpath('//div/node()[last()]').extract()
Out[357]: ['\n']
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294