0
def parse(self, response):
    for line in response.css('small'):
        yield {
            'seats': line.css('*').get().re('\d')
        }

此代码将 mothing 放入我的文件中

但是这段代码:

def parse(self, response):
    for line in response.css('small'):
        yield {
            'seats': line.css('*').get()
        }

(相同但没有重新)

在文件中放了很多东西,包括很多数字。那么为什么第一个代码不给我数字呢?

4

1 回答 1

1

在第一个示例中,您错误地组合了get()re()方法。re()Selectorwhileget()从选择器返回文本数据的方法。因此,您需要应用re()到选择器本身:

def parse(self, response):
    for line in response.css('small'):
        yield {
            'seats': line.css('*').re(r'\d+')
        }

r'\d+'另外,请注意对正则表达式模式使用原始字符串,并将\d+其作为返回所有数字的实际模式。

于 2020-03-01T09:19:45.637 回答