1. 前言
大家好,我是安果!
众所周知,Python 最流行的爬虫框架是 Scrapy,它主要用于爬取网站结构性数据
今天推荐一款更加简单、轻量级,且功能强大的爬虫框架:
feapder
项目地址:
https://github.com/Boris-code/feapder
2. 介绍及安装
和 Scrapy 类似,feapder 支持轻量级爬虫、分布式爬虫、批次爬虫、爬虫报警机制等功能
内置的 3 种爬虫如下:
在实战之前,我们在虚拟环境下安装对应的依赖库
# 安装依赖库
pip3 install feapder
3. 实战一下
我们以最简单的 AirSpider 来爬取一些简单的数据
目标网站:
aHR0cHM6Ly90b3BodWIudG9kYXkvIA==
详细实现步骤如下( 5 步)
3-1
创建爬虫项目
首先,我们使用「
feapder create -p
」命令创建一个爬虫项目
# 创建一个爬虫项目
feapder create -p tophub_demo
3-2
创建爬虫
AirSpider
命令行进入到 spiders 文件夹目录下,使用「 feapder create -s 」命令创建一个爬虫
cd spiders
# 创建一个轻量级爬虫
feapder create -s tophub_spider 1
其中
3-3
配置数据库、创建数据表、创建映射 Item
以 Mysql 为例,首先我们在数据库中创建一张数据表
# 创建一张数据表
create table topic
(
id int auto_increment
primary key,
title varchar(100) null comment '文章标题',
auth varchar(20) null comment '作者',
like_count int default 0 null comment '喜欢数',
collection int default 0 null comment '收藏数',
comment int default 0 null comment '评论数'
);
然后,打开项目根目录下的 settings.py 文件,配置数据库连接信息
# settings.py
MYSQL_IP = "localhost"
MYSQL_PORT = 3306
MYSQL_DB = "xag"
MYSQL_USER_NAME = "root"
MYSQL_USER_PASS = "root"
最后,创建映射 Item( 可选 )
进入到 items 文件夹,使用「
feapder create -i
」命令创建一个文件映射到数据库
PS:
由于 AirSpider 不支持数据自动入库,所以这步不是必须
3-4
编写爬虫及数据解析
第一步,首先使「
M
ysqlDB
」
初始化数据库