content = hxs.select('//*[@id="content"]')
content = content.select('string()').extract()[0]
cleaned_content = re.sub('(\\s)\\s+', '\\1', content, flags=re.MULTILINE +
    re.UNICODE)
texts = '%s\n\n%s' % (title, cleaned_content)
with codecs.open(filename, 'w', encoding='utf-8') as output:
    output.write(texts)
Formatting text output with Scrapy in Python