【scrapy框架】爬取内容后写入数据库

0 基本逻辑

python 复制代码
1 创建项目 scrapy startproject 项目名字
2 cd 到spiders文件夹下
3 创建爬虫文件
  scrapy genspider -t crawl 爬虫文件名字 爬取的域名

1 settings.py文件中设置日志文件

python 复制代码
# 一般不采取这种方式
# LOG_LEVEL = 'WARNING' 
# 推荐使用日志文件的方式
LOG_FILE = 'log.log'

2 使用scrapy爬取读书网的中书的名字和图片地址

2.1 新建项目

python 复制代码
scrapy startproject 项目名字

2.2 新建爬虫名字

python 复制代码
scrapy genspidef 爬虫名字 域名  # 域名,如www.baidu.com

2.3 在爬虫文件中写爬取逻辑

python 复制代码
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from read_book.items import ReadBookItem


class ReadbookSpider(CrawlSpider):
    name = "read_book"
    allowed_domains = ["www.dushu.com"]
    start_urls = ["https://www.dushu.com/book/1188_1.html"]

    rules = (
        Rule(LinkExtractor(allow=r"/book/1188_\d+\.html"),
                          callback="parse_item",
                          follow=True),  
    )

    def parse_item(self, response):
        img_list = response.xpath('//div[@class="bookslist"]//img')
        for img in img_list:
            name = img.xpath('./@data-original').extract_first()
            src = img.xpath('./@alt').extract_first()

            book = ReadBook101Item(name=name, src=src)
            yield book

2.4 items.py文件中

python 复制代码
import scrapy


class ReadBookItem(scrapy.Item):
 
    name = scrapy.Field()
    src = scrapy.Field()

2.5 pipelines.py文件中

python 复制代码
from itemadapter import ItemAdapter
from scrapy.utils.project import get_project_settings  # 加载settings文件
import pymysql


class ReadBook101Pipeline:
    def open_spider(self, spider):
        self.fp = open('book.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        self.fp.write(str(item))
        return item

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


class MysqlPipeline:

    def open_spider(self, spider):
        settings = get_project_settings()
        self.host = settings['DB_HOST']
        self.user = settings['DB_USER']
        self.password = settings['DB_PASSWORD']
        self.name = settings['DB_NAME']
        self.port = settings['DB_PORT']
        self.charset = settings['DB_CHARSET']

        self.connect()

    def connect(self, ):
        self.conn = pymysql.connect(
            user=self.user,
            password=self.password,
            host=self.host,
            database=self.name,
            port=self.port,
            charset=self.charset,
        )

        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        sql = 'insert into book(name,src) values("{}","{}")'.format(
            item['name'], item['src']
        )
        self.cursor.execute(sql)
        self.conn.commit()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.conn.close()

2.6 settings文件中开启管道、配置数据库

python 复制代码
DB_HOST = '127.0.0.1'
DB_PORT = 3306
DB_USER = 'root'
DB_PASSWORD = 'root'
DB_NAME = 'spider01'
# utf-8不允许使用 - 否则会报错NoneType......
DB_CHARSET = 'utf8'


# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   "read_book_101.pipelines.ReadBook101Pipeline": 300,
   'read_book_101.pipelines.MysqlPipeline': 301
}
相关推荐
星星也在雾里10 小时前
PgBouncer 解决 PostgreSQL 连接数超限 + 可视化监控
数据库·postgresql
雨辰AI12 小时前
SpringBoot3 + 人大金仓读写分离 + 分库分表 + 集群高可用 全栈实战
java·数据库·mysql·政务
长城202412 小时前
关于MySql的ONLY_FULL_GROUP_BY问题
数据库·mysql·聚合列
常常有13 小时前
MySQL 底层执行原理:输入SQL语句到两阶段提交
数据库·sql·mysql
Mr. zhihao13 小时前
深入解析redis基本数据结构
数据结构·数据库·redis
m0_7488394913 小时前
利用天正暖通CAD快速掌握风管数量统计的方法
数据库
随身数智备忘录13 小时前
什么是设备管理体系?设备管理体系包含哪些核心模块?
网络·数据库·人工智能
海市公约14 小时前
MySQL更新语句执行全流程:从Buffer Pool修改到二阶段提交
数据库·mysql·binlog·innodb·undo log·二阶段提交·update执行原理
颂love14 小时前
MySQL的执行流程
android·数据库·mysql
程序leo源15 小时前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#