golang如何实现抓取IP地址的蜘蛛程序详解

  var vs [] interface{} //存储输入参数的接口数组

  var vss string //待拼接的输入参数占位符字符串

  for _, el := range ip { //处理所有的数据

  vs = append(vs, el[1], el[2], province) //每一列包括开始地址、结束地址与省份

  vss += "(?,?,?)," //占位符

  }

  vss = vss[0:len(vss) -1] //去掉最后的逗号

  var configs interface{} //从配置文件取数据库信息

  fr, err := os.Open("http://www.jb51.net/article/configs.json") //配置文件内容请参照上篇文章《golang实现mysql数据库备份》

  if err != nil {

  ch <- err.Error()

  return err

  }

  decoder := json.NewDecoder(fr)

  err = decoder.Decode(&configs)

  confs := configs.(map[string]interface{})

  dialect := confs["database_dialect"].(string)

  dbConf := confs["db_"+dialect+"_config"].(map[string]interface{})

  dbHost := dbConf["db_host"].(string)

  dbPort := strconv.FormatFloat(dbConf["db_port"].(float64), 'f', -1, 32)

  dbUser := dbConf["db_user"].(string)

  dbPass := dbConf["db_pass"].(string)

  dbName := dbConf["db_name"].(string)

  dbCharset := dbConf["db_charset"].(string)

  dao, err := mysql.Open(dialect, dbUser + ":"+dbPass+"@tcp("+dbHost+":"+dbPort+")/"+dbName+"?charset="+dbCharset)

  defer dao.Close()

  if err != nil {

  ch <- err.Error()

  return err

  }

  //批量插入语句拼接

  sqlstr := "insert into ip_addr_info (ip_addr_begin,ip_addr_end,province) values " + vss

  stmt, err := dao.Prepare(sqlstr) //预处理带参数的sql语句

  rs, err := stmt.Exec(vs...) //带参数执行sql语句

  if err != nil { //出错,返回错误信息

  ch <- err.Error()

  return err

  }else { //成功,返回成功信息

  affect, _ := rs.RowsAffected()

  ch <- "Province: " + province + ", affect: " + strconv.FormatInt(affect, 10)

  return nil

  }