存入MySQL的相關操作與配置

來源:互聯網
上載者:User

標籤:img   檔案   import   提交   inf   圖片   iter   啟用   screen   

#匯入settings檔案中的配置
from scrapy.utils.project import get_project_settings
class MyMySqlPipeline(object):
def open_spider(self, spider):
# 串連資料庫,需要在settings.py 檔案中配置
settings = get_project_settings()
host = settings[‘DB_HOST‘]
port = settings[‘DB_PORT‘]
user = settings[‘DB_USER‘]
password = settings[‘DB_PASSWORD‘]
dbname = settings[‘DB_NAME‘]
dbcharset = settings[‘DB_CHARSET‘]
self.conn = pymysql.Connect(host=host, port=port, user=user, password=password, db=dbname, charset=dbcharset)
self.conn = pymysql.Connect(host=host, port=port, user=user, password=password, db=dbname, charset=dbcharset)

def process_item(self, item, spider):
# 寫入資料庫中
sql = ‘insert into movies(movie_poster, movie_name, movie_score, movie_type, movie_director, movie_screenwriter, movie_actor, movie_time, movie_content) values("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")‘% (
item[‘movie_poster‘], item[‘movie_name‘], item[‘movie_score‘], item[‘movie_type‘], item[‘movie_director‘], item[‘movie_screenwriter‘], item[‘movie_actor‘],
item[‘movie_time‘], item[‘movie_content‘])
# 執行sql語句
self.cursor = self.conn.cursor()
try:
       #建立遊標
self.cursor.execute(sql)
       #提交操作
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
return item

def close_spider(self, spider):
self.cursor.close()
self.conn.close()
注意:另外需要在資料庫中定義資料結構,應為mysql不會自己建立

在settings.py 中任意位置配置MySQL

 啟用使用

也可以在爬蟲檔案中自訂

 

 



存入MySQL的相關操作與配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.