如何在 python scrapy 中只获取文本

How can I get only text in python scrapy

我正在尝试获取数据 returned 但是 我的代码:

    def parse(self, response, **kwargs):
    Name = response.xpath( "//h1//text()" ).get(),

但是 Name 的数据 return 是:

('Name product',)

如何才能只得到'Example'?

Name product

谢谢

将您的代码更改为以下内容(a.k.a 删除您在“.get()”之后放置的逗号。

def parse(self, response, **kwargs):
    Name = response.xpath( "//h1//text()" ).get()

请注意,逗号可能是一个有用的功能,可以简洁地 return 多个值(如果您还不知道)

查看 scrapy docs,我看到:

.get() always returns a single result; if there are several matches, content of a first match is returned; if there are no matches, None is returned. .getall() returns a list with all results.

所以它应该return一个字符串,而不是一个元组。

在您的代码中,我在最后一行的末尾看到一个逗号:.get(),。这可能是将字符串转换为元组。