1. 程式人生 > >mac在pycharm下debug時, import MySQLdb 報這個錯誤,怎麼解決?

mac在pycharm下debug時, import MySQLdb 報這個錯誤,怎麼解決?

mac下抓取伯樂線上網站debug過程中遇到的坑及補充

使用vartualenv建立的python3.x版本在pycharm中執行第四章抓取伯樂線上的例子中

  • 如果遇到以下情況該怎麼做
    1.ImportError: dlopen(/Users/apple/.virtualenvs/fortune_spider/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so, 2): Library not loaded: libssl.1.0.0.dylib Referenced from: /Users/apple/.virtualenvs/fortune_spider/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so Reason: image not found

解決辦法:
放棄使用MySQLdb,直接使用pymysql。直接在pycharm中安裝即可。
在這裡插入圖片描述
程式碼微調:
在這裡插入圖片描述
這個方法是最快捷簡單的,因為mysqldb對mac的友好度太低了。
2.按照老師的資料庫設計,你會報很多錯,我也不知道為啥老師沒有自增長可以爬取到資料,我實驗一晚上也一直報沒有自增長的錯誤和url_object_id沒有預設值的錯誤,例如:
在這裡插入圖片描述
下面貼出我的資料庫表

DROP TABLE IF EXISTS `jobbole_article`;
CREATE TABLE `jobbole_article`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `create_date` date NULL DEFAULT NULL,
  `url` varchar(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `url_object_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 0 ,
  `front_image_url` varchar(300) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `front_image_path` varchar(300) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `comment_nums` int(11) NOT NULL DEFAULT 0,
  `fav_nums` int(11) NOT NULL DEFAULT 0,
  `praise_nums` int(11) NOT NULL DEFAULT 0,
  `tags` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `content` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

這樣這個問題就可以解決了,你們直接run main, 不用debug了,資料就儲存進去了。
3.scrapy中怎麼進行非同步插入資料庫mysql

class MysqlTwistedPipline(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host = settings["MYSQL_HOST"],
            db = settings["MYSQL_DBNAME"],
            user = settings["MYSQL_USER"],
            passwd = settings["MYSQL_PASSWORD"],
            charset='utf8',
            cursorclass=cursors.Cursor,
            use_unicode=True,
        )
        dbpool = adbapi.ConnectionPool("pymysql", **dbparms)

        return cls(dbpool)

    def process_item(self, item, spider):
        #使用twisted將mysql插入變成非同步執行
        query = self.dbpool.runInteraction(self.do_insert,item)
        query.addErrback(self.handle_error, item, spider) #處理異常

    def handle_error(self, failure, item, spider):
        #處理非同步插入的異常
        print (failure)

    def do_insert(self, cursor, item):
        #執行具體的插入
        #根據不同的item 構建不同的sql語句並插入到mysql中
        insert_sql = """
                   insert into jobbole_article(title, url, create_date, fav_nums)
                   VALUES (%s, %s, %s, %s)
               """
        cursor.execute(insert_sql, (item["title"], item["url"], item["create_date"], item["fav_nums"]))

class JsonExporterPipleline(object):
    #呼叫scrapy提供的json export匯出json檔案
    def __init__(self):
        self.file = open('articleexport.json', 'wb')
        self.exporter = JsonItemExporter(self.file, encoding="utf-8", ensure_ascii=False)
        self.exporter.start_exporting()

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item