本文實(shí)例講述了MySQL無法存儲(chǔ)emoji表情解決方案。分享給大家供大家參考,具體如下:
今天學(xué)習(xí)爬蟲爬伯樂在線的文章,由于在文章中有emoji表情,導(dǎo)致有emoji表情的文章都爬取不下來
經(jīng)過一番搜索之后終于解決了問題。
相關(guān)文章可參考:
①. MySQL無法存儲(chǔ)Emoji表情問題
②. mysql存emoji表情報(bào)錯(cuò)處理
1. 在navicat中
如果在新建表之前就改變數(shù)據(jù)庫(kù)的編碼,建表的時(shí)候好像可以自己轉(zhuǎn)變過來吧
查看字符集編碼:
show variables like '%char%';
更改數(shù)據(jù)庫(kù)編碼:
ALTER DATABASE 數(shù)據(jù)庫(kù)名 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
更改表編碼:
ALTER TABLE 表名 CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
下面這些,好像沒用到,要是不行也可以用用
set @@character_set_client='utf8mb4';set @@character_set_connection='utf8mb4';set @@character_set_results='utf8mb4';set @@character_set_server='utf8mb4';
2. 在爬蟲代碼中
class MysqlPipeline(object): def __init__(self): self.conn = MySQLdb.connect('127.0.0.1', 'root', 'root', 'article_spider', charset='utf8mb4', use_unicode=True) self.cursor = self.conn.cursor() def process_item(self, item, spider): insert_sql = ''' insert into article(title,url,bookmark_nums,url_object_id,content) VALUE (%s,%s,%s,%s,%s) ''' self.cursor.execute(insert_sql, (item['title'], item['url'], item['bookmark_nums'],item['url_object_id'], item['content'])) self.conn.commit()希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選