这里我使用的是以下环境
python的版本:3.6
scrapy的版本:1.5
coding:utf-8
import scrapy
import codecs
class TestSpider(scrapy.Spider):
name = 'yilian_spider'
start_urls = [
'http://lab.scrapyd.cn/page/1/',
]
def parse(self,response):
filename = 'yilian.txt'
text1 = response.css('div .quote')[0]
content = text1.css('.text::text').extract_first()
author = text1.css('.author::text').extract_first()
tags = text1.css('.tags .tag::text').extract()
tags = ','.join(tags)
f = open(filename,'a+')
f.write('标签:'+tags)
f.write('\n')
f.write('作者:'+author)
f.write('\n')
f.write('内容:'+content) #写内容
f.write('\n')
f.close()
print('保存文件了')
网友评论