使用scrapy爬取古诗文网的前十页数据

使用scrapy爬取古诗文网的前十页数据

创建scrapy框架

使用cmd创建一个爬虫项目

scrapy startproject gsww   #创建新项目

然后进入目录中,创建spider

cd gsww 
scrapy genspider gsww_spider www.gushiwen.cn

在这里插入图片描述

设置scrapy项目

在settings的程序里面设置

ROBOTSTXT_OBEY = False  #设置不遵守robots协议

在这里插入图片描述

DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/5',
  'Accept-Language': 'en',
}  #设置请求头的headers

在这里插入图片描述

ITEM_PIPELINES = {
   'gsww.pipelines.GswwPipeline': 300,
}

在这里插入图片描述

写爬虫类

class GswwSpiderSpider(scrapy.Spider):
    name = 'gsww_spider'
    allowed_domains = ['www.gushiwen.cn']
    start_urls = ['https://www.gushiwen.cn/default_1.aspx']
    page = 1

    def myprint(self, value):
        print('='*30)
        print(value)
        print('='*30)


    def parse(self, response):
        gsw_divs = response.xpath("//div[@class='left']/div[@class='sons']")
        for gsw_div in gsw_divs:
            # self.myprint(type(response))
            # response.xpath返回SelectorList对象
            title = gsw_div.xpath('.//b/text()').getall()
            title = ''.join(title)
            # self.myprint(title)
            dynasty = gsw_div.xpath('.//p[@class="source"]/a[1]/text()').getall()
            dynasty = ''.join(dynasty)
            author = gsw_div.xpath('.//p[@class="source"]/a[2]/text()').getall()
            author = ''.join(author)
            # 下面的//text()代表的是获取class='contson'下面所有的子孙文本
            content_list = gsw_div.xpath(".//div[@class='contson']//text()").getall()
            # self.myprint(content_list)
            content = "" .join(content_list).strip()
            self.myprint(content)
            item = GswwItem(title=title, dynasty=dynasty, author=author, content=content)
            yield item

设置爬取的内容

class GswwItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field()
    dynasty = scrapy.Field()
    author = scrapy.Field()
    content = scrapy.Field()  

在这里插入图片描述

保存数据

import json
class GswwPipeline:
    def open_spider(self, spider):
        self.fp = open("古诗文.txt", 'w', encoding='utf-8')

    def process_item(self, item, spider):
        self.fp.write(json.dumps(dict(item),ensure_ascii=False) + "\n")
        return item

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

by 习惯于一个人面对所有

标题设置多页爬取(在gsww_spider.py里面设置)

 next_url = response.xpath('//a[@id="amore"]/@href').get()
        print(next_url)
        if not next_url:
            return
        else:
            yield scrapy.Request('https://www.gushiwen.cn' + next_url, callback=self.parse)
            # scrapy.Request(这个网址一定要是str类型的,所以前面就不能使用getall方法来获取,getall方法获取的是一个列表)

在这里插入图片描述

程序运行效果

为了方便我们运行方便,可以自己写一个程序放到项目的根目录下

from scrapy import cmdline #导入scrapy下面的cmdline包
# 调用cmdline.execute方法执行运行命令
cmdline.execute("scrapy crawl gsww_spider".split(' '))

在这里插入图片描述

运行效果:
在这里插入图片描述

最后把源码链接贴出来:
https://download.csdn.net/download/qiaoenshi/12913580

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值