Scrapy-从所有匹配的div中获取文本

时间:2019-01-09 05:47:02

标签: scrapy scrapy-spider

我需要从所有具有“ feature has-feature”类的div中获取文本

<div class="features as-columns list">
    <div class="feature has-feature">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
            <path d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"></path>
        </svg>
        "What If" Scenarios
    </div>
    <div class="feature ">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
            <path d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"></path>
        </svg>
        Audit Trail
    </div>
    <div class="feature has-feature">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
            <path d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"></path>
        </svg>
        Balance Sheet
    </div>
    <div class="feature ">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
            <path d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"></path>
        </svg>
        Multi-Department / Project
    </div>
</div>
  

我正在使用以下代码,但返回的是空白字符串。可能是因为   之间的svg标签:

product_features = ""
divs = response.xpath("//div[@class='feature has-feature']")
for div in divs:
    product_features = product_features + div.xpath("./text()").extract_first().strip() + "|"
product_features = product_features.strip("|")

3 个答案:

答案 0 :(得分:1)

您可以简称为CSS选择器。进行检查,例如:

>>> [i.strip() for i in response.css("div.feature.has-feature ::text").extract() if i.strip()]
[u'"What If" Scenarios', u'Balance Sheet']

答案 1 :(得分:1)

for div in divs:
    product_features = product_features + div.xpath("./text()").extract_first().strip() + "|"
product_features = product_features.strip("|")

您使用的是extract_first(),它返回第一个元素,而如果您检查extract(),则会有三个值为[u'\n ', u'\n "What If" Scenarios\n ']

要获得价值,

txt = [val for val in div.xpath("./text()").extract() if val.strip()]
product_features = product_features + txt + '|'
product_features = product_features.strip('|')

答案 2 :(得分:0)

In [1]: [text.strip() for text in response.xpath("//div[@class='feature has-feature']/text()[normalize-space()]").extract()]
Out[1]: ['"What If" Scenarios', 'Balance Sheet']

(一个不错的XPath速查表可以在这里找到:https://devhints.io/xpath