Scrapy 爬虫实战-爬取字幕库

Scrapy 爬虫实战-爬取字幕库


1.首先,创建Scrapy框架

创建工程
scrapy startproject zimuku

创建爬虫程序
cd zimuku
scrapy genspider zimu zimuku.cn

如图:
在这里插入图片描述
在这里插入图片描述
我们会发现所有的框架以及模板都已经创建好了,
依次给大家看看:

zimu.py
# -*- coding: utf-8 -*-
import scrapy


class ZimuSpider(scrapy.Spider):
    name = 'zimu'
    allowed_domains = ['zimuku.cn']
    start_urls = ['http://zimuku.cn/']

    def parse(self, response):
        pass

items.py
 -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class ZimukuItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

pipelines.py
# -*- coding: utf-8 -*-

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


class ZimukuPipeline(object):
    def process_item(self, item, spider):
        return item

这是三个比较重要,其他的我先不一一列举了。
接下来,我们要进行分析网页了

2.网页分析
在这里插入图片描述
我们的目的是把这个内容保存下来,因为Scrapy框架自带一下工具,所以我们就用xpath来做内容匹配。
提取红框的内容的xpath语句为:/html/body/div[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a/b/text()

3.编写代码
(1)首先编写items.py

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

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class ZimukuItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    #要爬取的内容定义
    text = scrapy.Field()

(2)编写zimu.py

# -*- coding: utf-8 -*-
import scrapy
#需要把items中的类导进来
import zimuku.items import ZimukuItem

class ZimuSpider(scrapy.Spider):
    name = 'zimu'
    allowed_domains = ['zimuku.cn']
    start_urls = ['http://zimuku.cn/']

    def parse(self, response):
        '''
        :param response: 解析网页返回的内容
        :return: 
        '''
        name = response.xpath("/html/body/div[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a/b/text()")
        item = {}
        item['text'] = name
        yield item

(3)编写piplines(处理爬到的内容的)

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

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


class ZimukuPipeline(object):
    def process_item(self, item, spider):
        with open("F:\\python\\1.txt", 'a') as fp:
            fp.write(str(item['text']))
        print(item['text'])


(4)settings.py

#通过配置告诉Scrapy明白是谁来处理结果
ITEM_PIPELINES={'zimuku.pipelines.ZimukuPipeline':300,}

(5)运行

#不打印日志
scrapy crawl zimu --nolog
#打印日志
scrapy crawl meiju 

建议最好打印日志,不然有些错误不会发现,除了问题还不知道出在哪块

在这里插入图片描述

我们会发现运行成功了,我们再看看文件是否保存成功
在这里插入图片描述
OK!!!终于成功了。
这篇Scrapy是一步一步做的,对自己还是入门的朋友都是不错的参考。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值