【爬虫】4.2 Scrapy 中查找 html 元素

这篇具有很好参考价值的文章主要介绍了【爬虫】4.2 Scrapy 中查找 html 元素。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

Xpath简介

1. Scrapy 的 Xpath 简介

(1)使用xpath查找HTML中的元素

2. Xpath 查找 html 元素

(2)"//"与"/"的使用

(3)使用"."进行Xpath连续调用

(4)extract与extract_first函数使用

(5)获取元素属性值

(6)获取节点的文本值

(7)多个文本节点值

(8)使用condition限定tag元素

(9)使用position()序号来确定所选择的元素

(10)使用"*"代表任何element元素,不包括Text、Comment的结点

(11)使用@*代表属性

(12)Xpath搜索元素的父结点

(13)搜索后面的兄弟结点

(14)搜索前面的兄弟结点


Xpath简介

  • XPath是一门在XMLHTML文档中查找信息的语言,通 路径表达式XML文档中选取节点或节点位置,可以用 来在XMLHTML文档中对元素和属性进行遍历。
  • Xpath节点类型:元素、属性、文本、命名空间、指令处理、注释及文档
  • xpath定位方式(路径表达式+索引+属性
  • 格式:/node1/node2/node3[1]/node4[@attribute[="value“]]
  • xpath索引值从1开始
Xpath 常用表达式
表达式
描述
例子
nodename
选取此节点
例: body ,选取 body 元素
/
绝对路径 , 表示当前节点的下一级节点元素。
例: /body ,当前节点下一级的 body 元素,
默认当前节点选取根元素
//
相对路径,全文档查找;
例: //title ,全文档搜索 title 元素 body//title,在 body 元素后代中搜索所有 title 元素。
.
当前节点
例: .//title ,在当前节点后代中搜索所有 title元素
@
选取属性
//node[@attribute] ,例包含 attribute 属性的节点node
*
通配符
/* ,绝对路径匹配任意节点, //* ,全文匹 配任意节点,@* ,匹配任意属性
Xpath 特有的Selector对象函数/方法
函数/方法
功能描述
extract()
获取对象的元素文本的列表
extract_first()
获取对象的元素文本的列表的第一个元素
"/@attrName"
/@attrName" 获取元素的属性节点对象,用extract() 获取属性值
"/text()"
获取元素的文本节点对象,用 extract() 获取文本值
"/tag[condition]"
获取符合限定条件的元素对象,其中 condition 是由这个 tag 的属性、
文本等计算出的一个逻辑值。多个限定条件如下:
"tag[condition1][condition2]...[conditionN]" 或者:
"tag[condition1 and condition2 and ... and conditionN]"
position()
限定某元素对象,从 1 开始。可通过 and or 等构造复杂的表达式
"element/parent::*"
获取元素的父亲节点对象
" element/folllowing-sibling::*"
所有后序同级兄弟节点
“element/preceding-sibling::*“
所有前序同级兄弟节点

1. Scrapy 的 Xpath 简介

(1)使用xpath查找HTML中的元素

# 使用Xpath查找HTML中的元素
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book>
        <title lang="eng">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book>
        <title lang="eng">Learning XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
print(type(selector))  # <class 'scrapy.selector.unified.Selector'>
print(selector)  # <Selector query=None data='<html><body>\n<bookstore>\n    <book>\n ...'>
s = selector.xpath("//title")  # 全文查找title  形成一个Selector的列表
print(type(s))  # <class 'scrapy.selector.unified.SelectorList'>
print(s)  # [<Selector query='//title' data='<title lang="eng">Harry Potter</title>'>, <Selector query='//title' data='<title lang="eng">Learning XML</title>'>]
from scrapy.selector import Selector
scrapy 中引入 Selector 类,这个类就是选择查找类。
selector=Selector(text=htmlText)
使用 htmlText 的文字建立 Selector类,就是装载 HTML 文档,文档装载后就形成一个   Selector对象,就可以使用 xpath 查找元素。
print(type(selector)
可看到 selector 是一个类型为 scrapy.selector.unified.Selector,这个类型是一个有 xpath 方法的类 型。
s=selector.xpath("//title")
这个方法在文档中查找 所有 <title> 的元素,其中"//" 表示文档中的任何位置。一般地:
selector.xpath("//tagName")
表示在权文档中搜索<tagName> tags ,形成一个 Selector 的列表。
print(type(s))
由于 <title> 有两个元素,因此这是一个 scrapy.selector.unified.SelectorList类,
类似 scrapy.selector.unified.Selector的列表。
print(s)
s 包含两个 Selector 对象,一个是 <Selector xpath='//title' data='<title lang="eng">Harry Potter</title>'>,
另外一个是 <Selector xpath='//title' data='<title lang="eng">Learning XML</title>'>。
由此可见一般 selector 搜索一个 <tagName> HTML 元素的方法是:
selector.xpath("//tagName")
在装载 HTML 文档后 selector=Selector(text=htmlText)得到的 selector 是对应全文档顶层的元素<html> 的,其中 "//" 表示全文档搜索,结果是一个 Selector 的列表,哪怕只有一个元素也成一个列表,例如:
  • selector.xpath("//body") 搜索到<body>元素,结果是一个Selector的列表,包含一个Selector元素;
  •  selector.xpath("//title")搜索到两个<title>元素,结果是Selector 的列表,包含2Selector元素;
  • selector.xpath("//book")搜索到两个<book>元素,结果是Selector 的列表,包含2Selector元素

2. Xpath 查找 html 元素

(2)"//"与"/"的使用

        使用 “//” 表示文档下面的所有结点元素,用 “/” 表示当前结点的下一级结点元素

# "//"与"/"的使用
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book>
        <title lang="eng">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book>
        <title lang="eng">Learning XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
print(type(selector))
print(selector)
print("====================s1====================")
s1 = selector.xpath("//bookstore/book")  # 搜索<bookstore>下一级的<book>元素,找到2个
print(type(s1))
print(s1)
print("====================s2====================")
s2 = selector.xpath("//body/book")  # 搜索<body>下一级的<book>元素,结果为空
print(type(s2))
print(s2)
print("====================s3====================")
s3 = selector.xpath("//body//book")  # 搜索<body>下<book>元素,找到2个
print(type(s3))
print(s3)
print("====================s4====================")
s4 = selector.xpath("/body//book")  # 搜索文档下一级的<body>下的<book>元素,结果为空,∵文档的下一级是<html>元素,不是<body>元素
print(type(s4))
print(s4)
print("====================s5====================")
s5 = selector.xpath("/html/body//book")
# 或 s5 = selector.xpath("/html//book")  # 搜索<book>元素,找到2个
print(type(s5))
print(s5)
print("====================s6====================")
s6 = selector.xpath("//book/title")  # 搜索文档中所有<book>下一级的<title>元素,找到2个
print(type(s6))
print(s6)  # 结果与 selector.xpath("//title")   selector.xpath("//bookstore//title")一样
print("====================s7====================")
s7 = selector.xpath("//book//price")  # # 搜索文档中所有<book>下一级的<price>元素,找到2个
print(type(s7))
print(s7)  # 结果与 selector.xpath("//price")一样

运行结果:
<class 'scrapy.selector.unified.Selector'>
<Selector query=None data='<html><body>\n<bookstore>\n    <book>\n ...'>
====================s1====================
<class 'scrapy.selector.unified.SelectorList'>
[<Selector query='//bookstore/book' data='<book>\n        <title lang="eng">Harr...'>, <Selector query='//bookstore/book' data='<book>\n        <title lang="eng">Lear...'>]
====================s2====================
<class 'scrapy.selector.unified.SelectorList'>
[]
====================s3====================
<class 'scrapy.selector.unified.SelectorList'>
[<Selector query='//body//book' data='<book>\n        <title lang="eng">Harr...'>, <Selector query='//body//book' data='<book>\n        <title lang="eng">Lear...'>]
====================s4====================
<class 'scrapy.selector.unified.SelectorList'>
[]
====================s5====================
<class 'scrapy.selector.unified.SelectorList'>
[<Selector query='/html/body//book' data='<book>\n        <title lang="eng">Harr...'>, <Selector query='/html/body//book' data='<book>\n        <title lang="eng">Lear...'>]
====================s6====================
<class 'scrapy.selector.unified.SelectorList'>
[<Selector query='//book/title' data='<title lang="eng">Harry Potter</title>'>, <Selector query='//book/title' data='<title lang="eng">Learning XML</title>'>]
====================s7====================
<class 'scrapy.selector.unified.SelectorList'>
[<Selector query='//book//price' data='<price>29.99</price>'>, <Selector query='//book//price' data='<price>39.95</price>'>]

(3)使用"."进行Xpath连续调用

        使用 “.” 表示当前结点元素,使用 Xpath 可以连续调用,如果前一个 Xpath 返回一个 Selector 的列表,那么这个列表可以继续调用 Xpath 

        功能:为了每个列表元素调用 Xpath ,最后结果是全部元素调用 Xpath 的汇总

# 使用"."进行Xpath连续调用
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <title>books</title>
    <book>
        <title>Novel</title>
        <title lang="eng">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book>
        <title>TextBook</title>
        <title lang="eng">Learning XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book").xpath("./title")
# s = selector.xpath("//book").xpath("/title")  # 结果是空的,因为后面的 xpath("/title")从文档开始搜索<title>。
# s = selector.xpath("//book").xpath("//title")  # 结果有10个元素,因为每个 <book>都驱动xpath("//title")在全文档搜索 <title>元素,每次都搜索到5个元素。
for e in s:
    print(e)

运行结果:

<Selector query='./title' data='<title>Novel</title>'>
<Selector query='./title' data='<title lang="eng">Harry Potter</title>'>
<Selector query='./title' data='<title>TextBook</title>'>
<Selector query='./title' data='<title lang="eng">Learning XML</title>'>

        selector.xpath("//book") 首先搜索到文档中所有 <book> 元素,总共有2 个,然后再次调用 xpath("./title") ,就是从当前元素 <book> 开始往下一级搜索<title> ,每个 <book> 都找到 2 <title> ,因此结果有 4 <title>。
注意: 如果 xpath 连续调用时不指定是从前一个 xpath 的结果元素开始的,那么默认是从全文档开始的,结果会不一样,例如:
s=selector.xpath("//book").xpath("/title") 结果是空的,因为后面的 xpath("/title")从文档开始搜索 <title>
s=selector.xpath("//book").xpath("//title") 结果有 10 个元素,因为每个 <book>都驱动 xpath("//title") 在全文档搜索 <title>元素,每次都搜索到 5 个元素。

(4)extract与extract_first函数使用

如果 xpath 返回的 Selector 对象列表
① 调用 extract() 函数会得到这些对象的 元素文本 列表
② 使用 extract_first() 获取列表中 第一个元素值 ,如果列表为空 extract_first() 的值为 None
而对于单一的一个 Selector 对象
① 调用 extract() 函数就可以得到 Selector 对象对应的元素的文本值。
单一的 Selector 对象没有 extract_first() 函数
# extract与extract_first函数使用
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book/price")
print(type(s), s)
s = selector.xpath("//book/price").extract()
print(type(s), s)
s = selector.xpath("//book/price").extract_first()
print(type(s), s)

运行结果:

<class 'scrapy.selector.unified.SelectorList'> [<Selector query='//book/price' data='<price>29.99</price>'>, <Selector query='//book/price' data='<price>39.95</price>'>]
<class 'list'> ['<price>29.99</price>', '<price>39.95</price>']
<class 'str'> <price>29.99</price>

由此可见:
s=selector.xpath("//book/price") 得到的是SelectorList列表;
s=selector.xpath("//book/price").extract() 得到的是<price>元素的Selector对象对 应的<price>元素的文本组成的列表,即:
['<price>29.99</price>', '<price>39.95</price>']
s=selector.xpath("//book/price").extrac_first() 得到的是<price>元素的文本组成的列表的第一个元素,是一个文本,即: <price>29.99</price>

(5)获取元素属性值

        xpath使用 "/@attrName " 得到一个 Selector 元素的 attrName 属性 节点对象,属性节点对象也是一个Selector 对象,通过 extract() 获取属性值。
# 获取元素属性值
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book").xpath("./@id")
print(s)  # [<Selector query='./@id' data='b1'>, <Selector query='./@id' data='b2'>]
print(s.extract())  # ['b1', 'b2']
for e in s:
    print(e.extract())  # b1 \n  b2

运行结果:

[<Selector query='./@id' data='b1'>, <Selector query='./@id' data='b2'>]
['b1', 'b2']
b1
b2

由此可见:
s=selector.xpath("//book/@id")
结果是 2 <book> id 属性组成的 SelectorList 列表, 即属性也是一个 Selector 对象
print(s.extract()) 结果是 <book> id 属性的两个 Selector 对象的属性文本值的列表,即['b1', 'b2']
for e in s:
      print(e.extract())
每个 e 是一个 Selector 对象,因此 extract() 获取对象的属性值。

(6)获取节点的文本值

        xpath使用 "/text()" 得到一个 Selector 元素包含的 文本值 ,文本值节点对象也是一个Selector 对象,通过 extract() 获取文本值。
# 获取节点的文本值
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book/title/text()")
print(s)
print(s.extract())  # ['Harry Potter', '学习 XML']
for e in s:
    print(e.extract())  # Harry Potter  \n  学习 XML

运行结果:

[<Selector query='//book/title/text()' data='Harry Potter'>, <Selector query='//book/title/text()' data='学习 XML'>]
['Harry Potter', '学习 XML']
Harry Potter
学习 XML

由此可见:
s=selector.xpath("//book/title/text()") 结果也是 SelectorList 列表,即文本也是一个节点
print(s.extract()) 结果是文本节点的字符串值的列表,即['Harry Potter', ' 学习 XML']
for e in s:
     print(e.extract())
每个 e 是一个 Selector 对象,因此 extract() 获取对象的属性值。 值得注意 的是如果一个 element 的元素包含的文本不是单一的文本,那么可能会 产生多个文本值。

(7)多个文本节点值

# 多个文本节点值
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english"><b>H</b>ary <b>P</b>otter</title>
        <price>29.99</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book/title/text()")
print(s)
print(s.extract())  # ['ary ', 'otter']
for e in s:
    print(e.extract())

运行结果:

[<Selector query='//book/title/text()' data='ary '>, <Selector query='//book/title/text()' data='otter'>]
['ary ', 'otter']
ary 
otter

由此可见 <title> 中的文本值包含 arry otter 两个。

(8)使用condition限定tag元素

# 使用condition限定tag元素
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book/title[@lang='chinese']/text()")
print(s.extract_first())  # 学习 XML
s = selector.xpath("//book[@id='b1']/title")
print(s.extract_first())  # <title lang="english">Harry Potter</title>

运行结果:

学习 XML
<title lang="english">Harry Potter</title>

由此可见:
s=selector.xpath("//book/title[@lang='chinese']/text()")
搜索 <book> 下面属性 lang="chinese" <title>
s=selector.xpath("//book[@id='b1']/title")
搜索属性 id="b1" <book> 下面的 <title>

(9)使用position()序号来确定所选择的元素

        xpath可以使用 position() 来确定其中一个元素的限制,这个选择序号 1 开始的 ,不是从 0 开始编号的,还可以通过 and or 等构造复杂的表 达式。
# 使用position()序号来确定所选择的元素
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book[position()=1]/title")  # 选择第一个<book>元素下的title
print(s.extract_first())  # <title lang="english">Harry Potter</title>
s = selector.xpath("//book[position()=2]/title")  # 选择第二个<book>元素下的title
print(s.extract_first())  # <title lang="chinese">学习 XML</title>

运行结果:

<title lang="english">Harry Potter</title>
<title lang="chinese">学习 XML</title>

(10)使用"*"代表任何element元素,不包括Text、Comment的结点

        xpath使用星号  "*"  代表任何 Element 节点, 不包括 Text Comment 的节 点。
# 使用"*"代表任何element元素,不包括Text、Comment的结点
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book id="b1">
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//bookstore/*/title")
print(s.extract())  # ['<title lang="english">Harry Potter</title>', '<title lang="chinese">学习 XML</title>']

运行结果:

['<title lang="english">Harry Potter</title>', '<title lang="chinese">学习 XML</title>']

        其中 s=selector.xpath("//bookstore/*/title")是搜索<bookstore>的孙子节点<title>, 中间隔开一层任何元素。

(11)使用@*代表属性

        xpath使用 "@*" 代表任何属性
# 使用@*代表属性
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book>
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//book[@*]/title")  # 搜索任何包含属性的<book>元素下面的<title>
print(s.extract())  # ['<title lang="chinese">学习 XML</title>']
s = selector.xpath("//@*")  # 搜索文档中所有属性结点
print(s.extract())  # ['english', 'b2', 'chinese']

运行结果:

['<title lang="chinese">学习 XML</title>']
['english', 'b2', 'chinese']

        其中: s=selector.xpath("//book[@*]/title") 是搜索任何 包含属性的 <book> 元素下面 的<title> ,结果搜索到第二个 <book>s=selector.xpath("//@*")是搜索文档中所有 属性节点

(12)Xpath搜索元素的父结点

        xpath使用 "element/parent::*" 选择 element 的父节点,这个节点只有一个。如果写成element/parent::tag ,就指定 element tag 父节点,除非 element的父节点正好为 <tag> 节点,不然就为[ ]
# Xpath搜索元素的父结点
from scrapy.selector import Selector

htmlText = '''
<html><body>
<bookstore>
    <book>
        <title lang="english">Harry Potter</title>
        <price>29.99</price>
    </book>
    <book id="b2">
        <title lang="chinese">学习 XML</title>
        <price>39.95</price>
    </book>
</bookstore>
</body></html>
'''
selector = Selector(text=htmlText)
s = selector.xpath("//title[@lang='chinese']/parent::*")  # 等价 /parent::book
print(s.extract())  # ['<book id="b2">\n        <title lang="chinese">学习 XML</title>\n        <price>39.95</price>\n    </book>']

运行结果:

['<book id="b2">\n        <title lang="chinese">学习 XML</title>\n        <price>39.95</price>\n    </book>']

        其中 s=selector.xpath("//title[@lang='chinese']/parent::*")是查找属性为 lang='chinese'的<title>元素的父节点,就是id="b2"的<book>元素节点。

(13)搜索后面的兄弟结点

        xpath使用 "element/folllowing-sibling::*" 搜索 element 后面的同级 的所有兄弟节点,使用 "element/folllowing-sibling::*[position()=1]" element 后面的同级的第一个兄弟节点。
# 搜索后面的兄弟结点
from scrapy.selector import Selector

htmlText = """<a>A1</a>
              <b>B1</b>
              <c>C1</c>
              <d>D<e>E</e></d>
              <b>B2</b>
              <c>C2</c>"""
selector = Selector(text=htmlText)
s = selector.xpath("//a/following-sibling::*")  # 搜素<a>结点后面的兄弟结点
print(s.extract())  # ['<b>B1</b>', '<c>C1</c>', '<d>D<e>E</e></d>', '<b>B2</b>', '<c>C2</c>']
s = selector.xpath("//a/following-sibling::*[position()=1]")  # 搜索<a>结点后面的第1个兄弟结点
print(s.extract())  # ['<b>B1</b>']
s = selector.xpath("//b[position()=1]/following-sibling::*")  # 搜索第一个<b>结点后面的兄弟结点
print(s.extract())  # ['<c>C1</c>', '<d>D<e>E</e></d>', '<b>B2</b>', '<c>C2</c>']
s = selector.xpath("//b[position()=1]/following-sibling::*[position()=1]")  # 搜索第一个<b>结点后面的第1个兄弟结点
print(s.extract())  # ['<c>C1</c>']

运行结果:

['<b>B1</b>', '<c>C1</c>', '<d>D<e>E</e></d>', '<b>B2</b>', '<c>C2</c>']
['<b>B1</b>']
['<c>C1</c>', '<d>D<e>E</e></d>', '<b>B2</b>', '<c>C2</c>']
['<c>C1</c>']

(14)搜索前面的兄弟结点

        xpath使用  "element/preceding-sibling::*"  搜索 element 前面的同级的所有兄弟节点,使用"element/preceding-sibling::*[position()=1]" 搜索 element前面的同级的第一个兄弟节点。
# 搜索前面的兄弟结点
from scrapy.selector import Selector

htmlText = """<a>A1</a>
              <b>B1</b>
              <c>C1</c>
              <d>D<e>E</e></d>
              <b>B2</b>
              <c>C2</c>"""
selector = Selector(text=htmlText)
s = selector.xpath("//a/preceding-sibling::*")
print(s.extract())  # []
s = selector.xpath("//b/preceding-sibling::*[position()=1]")  # 是所有<b>前面的第1个兄弟结点
print(s.extract())  # ['<a>A1</a>', '<d>D<e>E</e></d>']
s = selector.xpath("//b[position()=2]/preceding-sibling::*")  # 是第二个<b>前面的所有兄弟结点
print(s.extract())  # ['<a>A1</a>', '<b>B1</b>', '<c>C1</c>', '<d>D<e>E</e></d>']
s = selector.xpath("//b[position()=2]/preceding-sibling::*[position()=1]")  # 这里的position()=1指的是前1个兄弟结点
print(s.extract())  # ['<d>D<e>E</e></d>']

运行结果:

[ ]
['<a>A1</a>', '<d>D<e>E</e></d>']
['<a>A1</a>', '<b>B1</b>', '<c>C1</c>', '<d>D<e>E</e></d>']
['<d>D<e>E</e></d>']


下一篇文章:Xpath和CSS信息提取的方法异同点 文章来源地址https://www.toymoban.com/news/detail-484473.html

到了这里,关于【爬虫】4.2 Scrapy 中查找 html 元素的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深入理解Scrapy中XPath的`following-sibling`选择器

    在深入 following-sibling 选择器之前,先简单了解一下XPath。XPath是一种在XML文档中查找信息的语言。它同样适用于HTML文档,因为HTML是XML的一种形式。XPath使用路径表达式在XML文档中进行导航。在网络爬虫框架Scrapy中,XPath是一种常用的网页元素定位方法。 在XPath中, following-si

    2024年01月20日
    浏览(33)
  • xpath定位元素(F12开发者工具xpath定位、xPath Helper扩展程序xpath定位)

    对于UI自动化初学者来说,可能不知道如何下手,我们安装好了python、selenium等环境,如何去进行元素定位,如何编写自动化测试用例呢?今天就讲一下xpath元素定位。 1.F12开发者工具xpath定位 首先我们使用谷歌进行测试,打开需要测试的页面,点击键盘F12,进入开发者模式,

    2024年02月16日
    浏览(44)
  • 爬虫:Scrapy热门爬虫框架介绍

    结合自身经验和内部资料总结的Python教程,每天3-5章,最短1个月就能全方位的完成Python的学习并进行实战开发,学完了定能成为大佬!加油吧!卷起来! 全部文章请访问专栏:《Python全栈教程(0基础)》 再推荐一下最近热更的:《大厂测试高频面试题详解》 该专栏对近年

    2024年02月13日
    浏览(32)
  • 爬虫---scrapy爬虫框架(详细+实战)

    ​ 活动地址:CSDN21天学习挑战赛 1、基本功能 Scrapy 是一个适用爬取网站数据、提取结构性数据的应用程序框架,它可以应用在广泛领域:Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。通常我们可以很简单的通过 Scrapy 框架实现一个爬虫,抓取指

    2023年04月22日
    浏览(38)
  • Selenium使用xpath定位元素

    参考资料: 【web自动化测试】xpath元素定位 接下来是使用 xpath 必须掌握的核心知识点,只要掌握好这些知识点,基本上能定义到想要的元素。 // 开头表示相对路径, 也可以用 / 开头表示绝对路径,一般用相对路径。 [] 内表示属性条件 [@name=‘accout’] 表示获取name属性等于

    2023年04月08日
    浏览(37)
  • Selenium元素定位之xpath

    目录 XPATH 元素定位 xpath通过绝对路径定位元素 xpath通过相对路径定位元素 xpath通过元素属性定位元素 xpath通过属性值模糊匹配定位元素 xpath通过文本定位元素 xpath常见语法一览表 XPath 是用于在 XML 文档中定位节点的语言。由于 HTML 可以是 XML (XHTML) 的实现,Selenium 用户可以利

    2024年02月03日
    浏览(32)
  • 爬虫(四):Scrapy热门爬虫框架介绍

    结合自身经验和内部资料总结的Python教程,每天3-5章,最短1个月就能全方位的完成Python的学习并进行实战开发,学完了定能成为大佬!加油吧!卷起来! 全部文章请访问专栏:《Python全栈教程(0基础)》 再推荐一下最近热更的:《大厂测试高频面试题详解》 该专栏对近年

    2024年02月11日
    浏览(33)
  • JS javascript 点击鼠标 鼠标事件 获取元素 获取元素Xpath

    js代码,鼠标在页面点击时,记录元素的Xpath 代码:  

    2024年02月15日
    浏览(36)
  • 【Python_Scrapy学习笔记(一)】Scrapy框架简介

    Scrapy 框架是一个用 python 实现的为了爬取网站数据、提取数据的应用框架,使用 Twisted 异步网络库来处理网络通讯,可以高效的完成数据爬取。本文主要介绍 Scrapy 框架的构成与工作原理。 Windows安装: -m -pip install Scrapy 或使用 PyCharm 可视化界面安装。 爬虫文件Spider:负责数

    2023年04月23日
    浏览(43)
  • selenium通过xpath定位元素报错?

    1.xpath介绍 XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。而html中也应用了这种语言 ,所以 ,我们定位html页面元素时也会用到xpath这种方法 。 2.xpath定位方式 xpath主要通过以下四种方法定位 : 路径定位 属性定位 多属性定位 路径

    2024年04月09日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包