Python查询MySQL进行远程采集图片实例
生活随笔
收集整理的這篇文章主要介紹了
Python查询MySQL进行远程采集图片实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這是四五年以前做小說站采集圖片時寫過唯一一次 Python 代碼
#!/usr/bin/python #-*-coding:utf-8-*-import MySQLdb, os, socket, time; import MySQLdb.cursors; import urllibUser = '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();轉(zhuǎn)載于:https://www.cnblogs.com/zhouzme/p/5758541.html
總結(jié)
以上是生活随笔為你收集整理的Python查询MySQL进行远程采集图片实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 利用JAX-RS快速开发RES
- 下一篇: 作为软件设计师的2013年终总结