zoukankan      html  css  js  c++  java
  • Python查询MySQL进行远程采集图片实例

    这是四五年以前做小说站采集图片时写过唯一一次 Python 代码

    #!/usr/bin/python
    #-*-coding:utf-8-*-
                           
    import MySQLdb, os, socket, time;
    import MySQLdb.cursors;
    import urllib
                           
    User = 'root';
    Passwd = '123';
    Host = 'localhost';
    Db = 'database_name';
    conn = MySQLdb.connect(user=User,passwd=Passwd,host=Host,db=Db);
    mysql = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor);
                           
    mysql.execute("select * from image order by id asc");
    result = mysql.fetchall();
                           
    startTime = time.time();
                           
    timeout = 10; # in seconds
    socket.setdefaulttimeout(timeout);
                           
    for row in result:
      dir = os.path.dirname(row['path']);
                           
      if not os.path.exists(dir):
        os.makedirs(dir);
        os.chmod(dir, 0777);
        os.chmod(os.path.dirname(dir), 0777);
                           
      data = urllib.urlretrieve(row['url'], row['path']);
      os.chmod(row['path'], 0777);
                           
      mysql.execute("delete from image where id = %d", row['id']);
                             
      passTime = int(time.time()) - int(startTime);
      if passTime >= 1750:
        break;
                           
      print str(row['id']);
                           
    mysql.close();
    conn.close();

  • 相关阅读:
    jq绑定on事件无效
    数字以0补全
    redis常用操作
    mysql数据操作日常
    centos端口映射
    centos7防火墙操作
    mysql5.7order by问题
    centos无法上网解决方法
    面试题
    ztree 获取子节点所有父节点的name的拼接
  • 原文地址:https://www.cnblogs.com/zhouzme/p/5758541.html
Copyright © 2011-2022 走看看