会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132464个问题
Python 全系列/第一阶段:Python入门/序列 35150楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 35154楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 35156楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 35159楼

代码在这:

# -*- 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 爬虫开发/移动端爬虫开发- 35160楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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