欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

item數(shù)據(jù)model保存到數(shù)據(jù)庫中

1.如何將item 數(shù)據(jù)model保存到數(shù)據(jù)庫中

  • 首先在本地創(chuàng)建好MySQL數(shù)據(jù)庫,再數(shù)據(jù)庫中創(chuàng)建好數(shù)據(jù)表

# 創(chuàng)建數(shù)據(jù)庫
create database item_database;
set global validate_password_length = 1;
set global validate_password_policy = 0;
grant all on item_database.* to 'xkd'@'%' identified by '123456';
flush privileges;
# 根據(jù)item創(chuàng)建數(shù)據(jù)表
create table item (title varchar(255) not null, image_url varchar(255) not null, date date not null, image_path varchar(255) not null, url varchar(255) not null, url_id char(50) not null primary key);

2. 安裝Python MySQL驅動


pip install mysqlclient

3. 在settings文件中修改pipeline

  • 然后爬取頁面,進行頁面解析,返回item交由settings.py文件中定義好的pipelines處理

ITEM_PIPELINES = {
   # 'XKD_Dribbble_Spider.pipelines.XkdDribbbleSpiderPipeline': 300,
   # 當items.py模塊yield之后,默認就是下載image_url的頁面
   'XKD_Dribbble_Spider.pipelines.ImagePipeline': 1,
   'XKD_Dribbble_Spider.pipelines.MysqlPipeline': 2,
}

4. 新建pipeline,寫入item到MySQL中

  • 接著在pipelines.py文件中新建一個新的pipelines類,如MysqlPipeline,在這個類中初始化數(shù)據(jù)庫連接,重寫 process_item() 方法將item的字段讀取出來,再提交到數(shù)據(jù)中表中; 最后運行項目成功后,可以使用命令行工具查看數(shù)據(jù)是否插入成功;

class MysqlPipeline:
    def __init__(self):
        self.conn = MySQLdb.connect(host='localhost', user='xkd', password='123456', database='item_database', use_unicode=True, charset='utf8')
        self.cursor = self.conn.cursor()
    def process_item(self, item, spider):
        sql = 'insert into item(title, image_url, date, image_path, url, url_id)' \
              'values (%s, %s, %s, %s, %s, %s)'
        date = item['date']
        self.cursor.execute(sql, args=(item['title'], item['image_url'], date.strftime('%y-%m-%d'), item['image_path'], item['url'], item['url_id']))
        self.conn.commit()
        return item
    def spider_closed(self, spider):
        self.cursor.close()
        self.conn.close()

5.在本地搭建MySQL數(shù)據(jù)庫的步驟

  • 先創(chuàng)建數(shù)據(jù)庫: create database 數(shù)據(jù)庫名;

    成都做網(wǎng)站、成都網(wǎng)站設計的開發(fā),更需要了解用戶,從用戶角度來建設網(wǎng)站,獲得較好的用戶體驗。創(chuàng)新互聯(lián)公司多年互聯(lián)網(wǎng)經(jīng)驗,見的多,溝通容易、能幫助客戶提出的運營建議。作為成都一家網(wǎng)絡公司,打造的就是網(wǎng)站建設產(chǎn)品直銷的概念。選擇創(chuàng)新互聯(lián)公司,不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來訪用戶感受到浩方產(chǎn)品的價值服務。

  • 然后給用戶授權: grant all on 數(shù)據(jù)庫名.* to '用戶名'@'%' identified by '密碼';

  • 記得刷新MySQL的系統(tǒng)權限相關表: flush privileges;

  • 在進入創(chuàng)建好的數(shù)據(jù)庫根據(jù)item創(chuàng)建數(shù)據(jù)庫表: create table item(字段);

6.查看數(shù)據(jù)庫表

  • 首先登錄MySQL數(shù)據(jù)庫,命令行: mysql -u用戶名 -p密碼;

  • 然后選擇我們創(chuàng)建的數(shù)據(jù)庫,命令行: use 數(shù)據(jù)庫名;

  • 然后就可以查看數(shù)據(jù)庫表是否成功插入數(shù)據(jù),命令行: select * from item;

  • 當數(shù)據(jù)庫表中數(shù)據(jù)很多的時候,我們可以在查詢語句末尾加入一個 \G 參數(shù),橫向的表結構會轉為使用縱向表結構輸出,利于閱讀;

參考: https://www.9xkd.com/user/plan-view.html?id=1693196261

本文題目:item數(shù)據(jù)model保存到數(shù)據(jù)庫中
瀏覽地址:http://chinadenli.net/article40/jhgpho.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號ChatGPT網(wǎng)站營銷外貿網(wǎng)站建設全網(wǎng)營銷推廣網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設