美文网首页
scrapy css选择器使用

scrapy css选择器使用

作者: 奋斗live | 来源:发表于2018-06-02 14:52 被阅读0次

    这里我使用的是以下环境

    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('保存文件了')
    

    相关文章

      网友评论

          本文标题:scrapy css选择器使用

          本文链接:https://www.haomeiwen.com/subject/ktsrsftx.html