pyspider爬取數據存入mysql--2.測試數據庫能否連通


做一個簡單的測試,看數據能否存入mysql

 1 #!/usr/bin/env python
 2 # -*- encoding: utf-8 -*-
 3 # Created on 2017-10-26 16:03:21
 4 # Project: mysql_test
 5 
 6 from pyspider.libs.base_handler import *
 7 import MySQLdb
 8 
 9 class Handler(BaseHandler): 10 crawl_config = { 11  } 12 13 def __init__(self): 14 self.db = MySQLdb.connect(ip, name, password, tableName, charset='utf8') 15 16 @every(minutes=24 * 60) 17 def on_start(self): 18 try: 19 cursor = self.db.cursor() 20 sql = "insert into user(userName, userAge, userAddress) values ('pyspider',1,'園子')" 21  cursor.execute(sql) 22  cursor.close() 23  self.db.commit() 24  self.db.close() 25 except Exception, e: 26 print e 27 self.db.rollback()

 user表中已經插入了記錄:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM