scrapy框架的使用

  1. 安装scrapy
pip3 install scrapy
  1. 创建项目

(1)创建项目文件夹

scrapy startproject projectname

(2)进入projectname文件夹

cd projectname

(3)创建爬虫文件

scrapy genspider fear_and_greed '网址'

结构目录大概如下图:
在这里插入图片描述
3. 编写scrapy爬虫项目
(1)编写items.py

  • 编写我们需要爬取的数据字段
import scrapy


class ExponentItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    timestamp = scrapy.Field()
    value = scrapy.Field()
    ......
  • 编写fear_and_greed.py文件

这个文件里面主要写的是我们爬虫的解析、主要逻辑

import scrapy


class FearAndGreedSpider(scrapy.Spider):
    name = 'fear_and_greed'
    allowed_domains = ['http://xxxxxxxxxx.com']
    start_urls = ['http://xxxxxxxxxx.com']

    def parse(self, response):
        pass

  • 编写pipelines.py文件

这里是管道文件,就是我们采集到的数据通过yield信号,传递到这里,进行后续操作,比如存入数据库,写文件…

  • 打开settings.py 文件更改配置
# 将ROBOTSTXT_OBEY 中的True修改成False
# 默认是True,遵守robots.txt文件中的协议,遵守允许爬取的范围。
# 设置为False,是不遵守robo协议文件
ROBOTSTXT_OBEY = False 

启用管道
在这里插入图片描述

  1. 执行scrapy项目
    在这里插入图片描述
scrapy crawl fear_and_greed
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值