Python: Scrapy 导出原始数据而不是仅导出 text()?

Python: Scrapy exports raw data instead of text() only?

我正在从这个导出 class:

class MySpider(BaseSpider):
    name =  "dozen"
    allowed_domains = ["yahoo.com"]
    start_urls = ["http://finance.yahoo.com/q/is?s=SCMP+Income+Statement&annual"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        revenue = hxs.select('//td[@align="right"]')
        items = []
        for rev in revenue:
            item = DozenItem()
            item["Revenue"] = rev.xpath("./strong/text()")
            items.append(item)
        return items[:7]

得到这个:

[<HtmlXPathSelector xpath='./strong/text()' data=u'\n                            115,450\xa0\xa0\n '>]

但我只想要115,450

如果我将 .extract() 添加到 item["Revenue"] 行的末尾,它不会导出任何内容。

这是 html 的部分,其中包括我要获取的内容:

<tr>
<td colspan="2">
<strong>Total Revenue</strong>
</td>
<td align="right">
<strong>115,450&nbsp;&nbsp;</strong>
</td>
<td align="right">
<strong>89,594&nbsp;&nbsp;</strong>
</td>
<td align="right">
<strong>81,487&nbsp;&nbsp;</strong>
</td>
</tr>

我觉得你做得很好。你能显示 html 吗?

通常,我使用这个代码:

rev.xpath("./strong/text()").extract()[0].encode('utf8').strip()

您尝试对第一个选择使用过于宽泛的 Xpath 表达式。 像这样尝试:

def parse(self, response):
    revenue = response.xpath('//td[@align="right"]/strong/text()')
    items = []
    for rev in revenue:
        item = DozenItem()
        item["Revenue"] = rev.re('\d*,\d*')
        items.append(item)
    return items[:3]