scrapy 2.3 将选择器与正则表达式一起用
Selector 也有 .re() 使用正则表达式提取数据的方法。但是,与使用不同 .xpath() 或 .css() 方法, .re() 返回字符串列表。所以你不能构造嵌套的 .re() 电话。
下面是一个用于从 HTML code 以上:
>>> response.xpath('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
['My image 1',
'My image 2',
'My image 3',
'My image 4',
'My image 5']
另外还有一个助手在做往复运动 .get() (及其别名) .extract_first() 为 .re() 命名 .re_first() . 使用它只提取第一个匹配字符串:
>>> response.xpath('//a[contains(@href, "image")]/text()').re_first(r'Name:\s*(.*)')
'My image 1'