Expressão | Descrição |
---|---|
/ |
Dá o nó raiz |
// |
Dá todos os nós |
. |
Dá o nó atual |
.. |
Dá o nó pai |
@ |
Dá atributos |
[n] |
Dá o enésimo elemento |
[last()] |
Dá o último elemento |
[last()-n] |
Dá o enésimo elemento do último |
[position()<n] |
Dá os primeiros n elementos |
[x>n] |
Dá todos os elementos x contendo um elemento maior que n |
$x('/html')
[
<
html
>
...
</
html
>
]
$x('/html/body')
[
<
body
>
...
</
body
>
]
$x('/html/body/div')
[
<
div
>
...
</
div
>
]
$x('/html/body/div/h1')
[
<
h1
>
Example Domain
</
h1
>
]
$x('/html/body/div/p')
[
<
p
>
...
</
p
>
,
<
p
>
...
</
p
>
]
$x('/html/body/div/p[1]')
[
<
p
>
...
</
p
>
]
$x('/html/body/div/p[2]')
[
<
p
>
...
</
p
>
]
$x('//html/head/title')
[
<
title
>
Example Domain
</
title
>
]
$x('//p')
[
<
p
>
...
</
p
>
,
<
p
>
...
</
p
>
]
$x('//a')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
$x('//div//a')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
$x('//div/a')
[ ]
$x('//a/text()')
[ "More information..." ]
$x('//div/*')
[
<
h1
>
Example Domain
</
h1
>
,
<
p
>
...
</
p
>
,
<
p
>
...
</
p
>
]
$x('//a[@href]')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
$x('//a[@href="http://www.iana.org/domains/example"]')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
$x('//a[contains(@href, "iana")]')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
$x('//a[starts-with(@href, "http://www.")]')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
$x('//a[not(contains(@href, "abc"))]')
[
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
]
response.xpath('/html').extract()
[u'
<
html
><
head
><
title
>
...
</
body
></
html
>
']
response.xpath('/html/body/div/h1').extract()
[u'
<
h1
>
Example Domain
</
h1
>
']
response.xpath('/html/body/div/p').extract()
[u'
<
p
>
This domain ... permission.
</
p
>
', u'
<
p
><
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
></
p
>
']
response.xpath('//html/head/title').extract()
[u'
<
title
>
Example Domain
</
title
>
']
response.xpath('//a').extract()
[u'
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
']
response.xpath('//a/@href').extract()
[u'http://www.iana.org/domains/example']
response.xpath('//a/text()').extract()
[u'More information...']
response.xpath('//a[starts-with(@href, "http://www.")]').extract()
[u'
<
a
href
="
http://www.iana.org/domains/example
">
More information...
</
a
>
']
Obtendo expressôes XPath do Chrome
- Clicar com o botao direito no elemento –> inspecionar elemento
- clique com o botao direito no inspetor –> copy –> copy Xpath
Este artigo foi útil ?
SimNão
Last modified: 14 de setembro de 2019