用Python Scrapy爬取某电影网站并存储入mysql

本文介绍了如何利用Python的Scrapy框架爬取javlib电影网站的数据,并详细阐述了从创建items.py定义数据结构,编写爬虫文件spidername.py进行爬取,到配置pipelines.py实现数据存储入MySQL数据库的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

爬取目标:javlib,使用框架Scrapy



首先使用在命令行里

scrapy startproject projectname
scrapy genspider spidername
指令创建爬虫。

首先定义items.py

import scrapy


class AvmoItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    pic = scrapy.Field()
    url = scrapy.Field()
    id_ = scrapy.Field()

这是spiders文件夹中的爬虫文件

spidername.py

# -*- coding: utf-8 -*-
import scrapy
import os
import sys
import re
import urllib.parse
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
import items


class JavbusSpider(scrapy.Spider):
    name = 'javlibrary'
    allowed_domains = ['www.ja14b.com']
    start_urls = ['http://www.ja14b.com/cn/vl_genre.php?g=cu']

    # 爬取目录页
    
使用 Scrapy 爬取古诗文数据存储MySQL 数据库中,可以按照以下步骤进行: 1. **安装必要的库**: 首先,确保你已经安装了 ScrapyMySQLPython 库。可以使用 pip 进行安装: ```bash pip install scrapy pymysql ``` 2. **创建 Scrapy 项目**: 使用 Scrapy 创建一个新的项目: ```bash scrapy startproject poetry_spider ``` 3. **定义 Item**: 在 `poetry_spider/items.py` 中定义你要爬取的字段,例如标题、作者和内容: ```python import scrapy class PoetrySpiderItem(scrapy.Item): title = scrapy.Field() author = scrapy.Field() content = scrapy.Field() ``` 4. **编写爬虫**: 在 `poetry_spider/spiders` 目录下创建一个新的爬虫文件,例如 `poetry_spider.py`: ```python import scrapy from poetry_spider.items import PoetrySpiderItem class PoetrySpider(scrapy.Spider): name = 'poetry' start_urls = ['http://www.example.com/poetry'] def parse(self, response): poems = response.css('div.poem') for poem in poems: item = PoetrySpiderItem() item['title'] = poem.css('h2::text').get() item['author'] = poem.css('p.author::text').get() item['content'] = poem.css('div.content::text').get() yield item next_page = response.css('a.next::attr(href)').get() if next_page: yield response.follow(next_page, self.parse) ``` 5. **配置 MySQL**: 在 `poetry_spider/settings.py` 中配置数据库连接信息: ```python import pymysql pymysql.install_as_MySQLdb() DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'poetry_db', 'USER': 'your_username', 'PASSWORD': 'your_password', 'HOST': 'localhost', 'PORT': '3306', } } ``` 6. **编写 Pipeline**: 在 `poetry_spider/pipelines.py` 中编写数据存储逻辑: ```python import pymysql class PoetrySpiderPipeline(object): def __init__(self): self.connection = pymysql.connect( host='localhost', user='your_username', password='your_password', db='poetry_db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) self.cursor = self.connection.cursor() def process_item(self, item, spider): sql = "INSERT INTO poems (title, author, content) VALUES (%s, %s, %s)" self.cursor.execute(sql, (item['title'], item['author'], item['content'])) self.connection.commit() return item def close_spider(self, spider): self.cursor.close() self.connection.close() ``` 7. **启用 Pipeline**: 在 `poetry_spider/settings.py` 中启用刚刚编写的 Pipeline: ```python ITEM_PIPELINES = { 'poetry_spider.pipelines.PoetrySpiderPipeline': 300, } ``` 8. **运行爬虫**: 最后,运行爬虫: ```bash scrapy crawl poetry ``` 这样,Scrapy 就会开始爬取古诗文数据存储MySQL 数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值