利用scrapy 做一个小说网站的爬虫,在利用pipline下载的时候,由于数据是一章一章获取的,如何把所有章节保存到一个docx中呢?
pipline的代码如下:
```python
import docx
from docx.oxml.ns import qn
class LuoqiuPipeline:
def __init__(self):
self.doc = docx.Document()
def process_item(self, item, spider):
nov_content = item['nov_content']
chapter_title = item['chapter_title']
self.doc.styles['Normal'].font.name = u'微软雅黑' # 可换成word里面任意字体
self.doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
self.doc.add_heading(chapter_title,level=3)
self.doc.add_paragraph(nov_content)
self.doc.save(chapter_title+'.docx')
return item
由于每章写进去以后都会重新建一个新的docx,最后的结果是: