偷瓜的贼 发表于 2018-9-28 10:18:49

Scrapy结合Mysql爬取天气预报入库

import MySQLdb  
import datetime
  

  
DEBUG = True
  

  
if DEBUG:
  
    dbuser = 'lihuipeng'
  
    dbpass = 'lihuipeng'
  
    dbname = 'game_main'
  
    dbhost = '192.168.1.100'
  
    dbport = '3306'
  
else:
  
    dbuser = 'root'
  
    dbpass = 'lihuipeng'
  
    dbname = 'game_main'
  
    dbhost = '127.0.0.1'
  
    dbport = '3306'
  

  
class MySQLStorePipeline(object):
  
    def __init__(self):
  
      self.conn = MySQLdb.connect(user=dbuser, passwd=dbpass, db=dbname, host=dbhost, charset="utf8", use_unicode=True)
  
      self.cursor = self.conn.cursor()
  
      #清空表:
  
      self.cursor.execute("truncate table yunweiApp_weather;")
  
      self.conn.commit()
  

  
    def process_item(self, item, spider):
  
      curTime =datetime.datetime.now()
  
      try:
  
            self.cursor.execute("""INSERT INTO yunweiApp_weather (weatherDate, weatherDate2, weatherWea, weatherTem1, weatherTem2, weatherWin, updateTime)
  
                            VALUES (%s, %s, %s, %s, %s, %s, %s)""",
  
                            (
  
                              item['weatherDate'].encode('utf-8'),
  
                              item['weatherDate2'].encode('utf-8'),
  
                              item['weatherWea'].encode('utf-8'),
  
                              item['weatherTem1'].encode('utf-8'),
  
                              item['weatherTem2'].encode('utf-8'),
  
                              item['weatherWin'].encode('utf-8'),
  
                              curTime,
  
                            )
  
            )
  

  
            self.conn.commit()
  

  

  
      except MySQLdb.Error, e:
  
            print "Error %d: %s" % (e.args, e.args)
  
      return item


页: [1]
查看完整版本: Scrapy结合Mysql爬取天气预报入库