项目加载器是一个很棒的工具,它们内置了 xpath 和 regex 的东西。
XPathItemLoader(response).get_xpath(xpath, regex)
http://doc.scrapy.org/en/latest/topics/loaders.html
>>> from scrapy.contrib.loader import XPathItemLoader
>>> response.body
'<html><body>\n<style type="text/css">\nimg.article_image[class] \n{\n...'
>>> from scrapy.contrib.loader import XPathItemLoader
>>> xl = XPathItemLoader(response=response, item={'image': ''})
>>> xl
<scrapy.contrib.loader.XPathItemLoader object at 0x7f5830079f50>
>>> xl.get_xpath('//style', re=r"background-image.*/([^/]+)'")
[u'image_1.png']
>>> xl.add_xpath('image', '//style', re=r"background-image.*/([^/]+)'")
>>> xl.load_item()
{'image': [u'image_1.png']}