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

代码在这:

# -*- 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 爬虫开发/移动端爬虫开发- 18836楼
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/归一化 18837楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6第一部分 18838楼

老师 我有点不明白 为什么输入http://localhost:8080/ 之后就会弹出这个界面呢?这其中的原理是什么?image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 18839楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 18840楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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