会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133624个问题
JAVA 全系列/第一阶段:AI驱动的JAVA编程/变量、数据类型、运算符 18872楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 18874楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 18875楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 18876楼
Python 全系列/第一阶段:Python入门/编程基本概念 18877楼
WEB前端全系列/第二阶段:JavaScript编程模块/函数与对象 18879楼
2019人工智能(隐藏)/第六阶段_非线性分类回归算法_决策树与经典集成学习算法/生成决策树所需分裂指标 18880楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 18881楼
Python 全系列/第十一阶段:Flask百战电商后台项目/Flask百战电商后台项目 18882楼

代码在这:

# -*- coding: utf-8 -*-
import scrapy


class BiqukuSpider(scrapy.Spider):
    name = 'biquku'
    allowed_domains = ['biquku.com']
    start_urls = ['https://www.biquku.co/5023/3234391.html']

    def parse(self, response):
        title=response.xpath('//h1/text()').extract_first()
        content=response.xpath('string(//div[@id="content"])').extract_first().strip()
        next_url=response.xpath('//a[@id="pager_next"]/@href').extract_first()

        yield {
            'title':title,
            'content':content
        }
        yield scrapy.Request(response.urljoin(next_url),callback=self.parse)#response.urljoin代表自动补齐url

下面是pipelines的

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class XiaoshuoPipeline(object):
    def open_spider(self,spider):
        self.filename=open('quanzhifashi.txt','w',encoding='utf-8')

    def process_item(self, item, spider):
        print(item)
        info=item['title']+'\n'+item['content']+'\n'
        self.filename.write(info)
        return item

    def close_spider(self,spider):
        self.filename.close()

settings的三个设置我也修改了,但是只能爬到一章。。。。。

Python 全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 18883楼
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/归一化 18884楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6第一部分 18885楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637