Alexander Yau Alexander Yau - 6 months ago 20
Javascript Question

xpath, how to filter href content

I am using

Scrapy
to crawl data.

On
JS
console on my browser, I type
$x('//div[@class="summary"]//div[contains(@class, "tags")]')
to get what I need, but I need to filter the data.

The following picture is the
$x('//div[@class="summary"]//div[contains(@class, "tags")]')
command result.

JS console result

How should I write
xpath
command to get the data in the green box? I tried
$x('//div[@class="summary"]//div[contains(@class, "tags")]//a[contains(@class, "post-tag")]')
, but that is not what I want。

Thank you!

Answer

To select inner text of <a> element within the selected div, you only need to append /a/text() to your XPath which selects the div :

//div[@class="summary"]//div[contains(@class, "tags")]/a/text()
Comments