简体   繁体   English

Scrapy ModuleNotFoundError:没有名为“MySQLdb”的模块

[英]Scrapy ModuleNotFoundError: No module named 'MySQLdb'

Just started out with Scrapy and I am trying to write to a MySQL database rather than outputting to a csv.刚开始使用 Scrapy,我正在尝试写入 MySQL 数据库而不是输出到 csv。

I have found the code here: https://gist.github.com/tzermias/6982723 that I am using to try to make this work, but unfortunately having an error that I can't get my head around.我在这里找到了代码: https : //gist.github.com/tzermias/6982723 ,我用它来尝试完成这项工作,但不幸的是出现了一个我无法理解的错误。

This is my pipelines.py:这是我的pipelines.py:

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

import MySQLdb.cursors
from twisted.enterprise import adbapi

from scrapy.xlib.pydispatch import dispatcher
from scrapy import signals
from scrapy.utils.project import get_project_settings
from scrapy import log

SETTINGS = get_project_settings()

class MySQLPipeline(object):

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.stats)

    def __init__(self, stats):
        #Instantiate DB
        self.dbpool = adbapi.ConnectionPool ('MySQLdb',
            host=SETTINGS['DB_HOST'],
            user=SETTINGS['DB_USER'],
            passwd=SETTINGS['DB_PASSWD'],
            port=SETTINGS['DB_PORT'],
            db=SETTINGS['DB_DB'],
            charset='utf8',
            use_unicode = True,
            cursorclass=MySQLdb.cursors.DictCursor
        )
        self.stats = stats
        dispatcher.connect(self.spider_closed, signals.spider_closed)
    def spider_closed(self, spider):
        """ Cleanup function, called after crawing has finished to close open
            objects.
            Close ConnectionPool. """
        self.dbpool.close()

    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self._insert_record, item)
        query.addErrback(self._handle_error)
        return item

def _insert_record(self, tx, item):
        result = tx.execute(
        """ INSERT INTO table VALUES (1,2,3)""" 
        )
        if result > 0:
            self.stats.inc_value('database/items_added')

def _handle_error(self, e):
    log.err(e)

This is what is in my settings.py:这是我的 settings.py 中的内容:

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'Website.pipelines.MySQLPipeline': 300,
}

#Database settings
DB_HOST = 'localhost'
DB_PORT = 3306
DB_USER = 'username'
DB_PASSWD = 'password'
DB_DB = 'scrape'

This is the spider.py:这是spider.py:

# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import SitemapSpider

class WebsitesitemapSpider(SitemapSpider):
    name = 'Websitesitemap'
    allowed_domains = ['Website.com']
    sitemap_urls = ['https://www.Website.com/robots.txt']

    def parse(self, response):
        yield {response.url}

I have been unable to find a working example of what I am looking to do to be able to work out where I am going wrong so thank you to anyone who looks at this or might be able to help.我一直无法找到我想要做的事情的工作示例,以便能够找出我出错的地方,因此感谢任何查看此内容或可能提供帮助的人。

do you have these packages installed "MySQLdb, scrapy, twisted".您是否安装了这些软件包“MySQLdb,scrapy,twisted”。

Else try installing using PIP and then try running the script.否则尝试使用 PIP 安装,然后尝试运行脚本。

you will need MySQL-python installed in your python environment, along with libmysql installed on the operating system.你需要在你的 python 环境中安装 MySQL-python,以及在操作系统上安装 libmysql。

On Ubuntu this would be achieved in the folllowing manner.在 Ubuntu 上,这将通过以下方式实现。

pip install MySQL-python sudo apt-get install libmysql-dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModuleNotFoundError: 没有名为“MySQLdb”的模块 - ModuleNotFoundError: No module named 'MySQLdb' ModuleNotFoundError:anaconda 中没有名为“MYSQLdb”的模块 - ModuleNotFoundError: No module named 'MYSQLdb' in anaconda sqlalchemy ModuleNotFoundError:没有名为“MySQLdb”的模块 - sqlalchemy ModuleNotFoundError: No module named 'MySQLdb' Scrapy ModuleNotFoundError:没有名为“import”的模块 - Scrapy ModuleNotFoundError: No module named "import" ModuleNotFoundError:没有名为“flask-mysqldb”的模块 - ModuleNotFoundError: No module named 'flask-mysqldb' scrapy import itemloaders ModuleNotFoundError:没有名为“itemloaders”的模块 - scrapy import itemloaders ModuleNotFoundError: No module named 'itemloaders' ModuleNotFoundError:没有名为“ scrapy_user_agents”的模块 - ModuleNotFoundError: No module named 'scrapy_user_agents' ModuleNotFoundError:没有名为“scrapy”的模块(PyCharm 中发生错误) - ModuleNotFoundError: No module named 'scrapy' (Error happend in PyCharm) ModuleNotFoundError:MAC OSX没有名为“ scrapy”的模块 - ModuleNotFoundError: No module named 'scrapy' for MAC OSX ModuleNotFoundError:没有名为“MySQLdb”的模块 Amazon MySQL RDS SQLAlchemy - ModuleNotFoundError: No module named 'MySQLdb' Amazon MySQL RDS SQLAlchemy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM