PythonのMySQLのクエリリモートキャプチャ画像例

それが書かれている小説ステーションは、画像は一度に4、5年前に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();

ます。https://my.oschina.net/zhouz/blog/213201で再現

おすすめ

転載: blog.csdn.net/weixin_34216107/article/details/91728492