You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

70 lines
2.3 KiB

import pymysql
from pymysql.cursors import DictCursor
from data_dict import special_symbols, escape_list
db_config = {
"host": "192.168.66.101",
"user": "root",
"passwd": "Sxzgx1209",
"port": 3306,
'database': 'scrapy'
}
class DbAction:
def __init__(self):
self.conn = pymysql.Connect(**db_config)
self.cursor = self.conn.cursor(cursor=DictCursor)
@staticmethod
def decode_pwd(_pwd):
for k, v in special_symbols.items():
if k in _pwd:
_pwd = _pwd.replace(k, v)
for item in escape_list:
if item[0] in _pwd:
_pwd = _pwd.replace(item[0], item[1])
return _pwd
def get_data_by_id(self, _id):
SELECT_SQL = "SELECT `id`, cate, `date`, name, unzip_pwd FROM scrapyh s WHERE s.id = %s;"
self.cursor.execute(SELECT_SQL, (_id,))
result = self.cursor.fetchone()
if result:
result['unzip_pwd'] = self.decode_pwd(result['unzip_pwd'])
return result
def get_available_pwd(self):
SELECT_SQL = "SELECT * FROM scrapyh_pwd sp ORDER BY sp.times DESC LIMIT 20;"
# SELECT_SQL = "SELECT * FROM scrapyh_pwd;"
self.cursor.execute(SELECT_SQL)
result = self.cursor.fetchall()
return [r['pwd'] for r in result]
def insert_pwd(self, _pwd):
if _pwd:
SELECT_SQL = "SELECT * FROM scrapyh_pwd sp WHERE sp.pwd = %s;"
self.cursor.execute(SELECT_SQL, (_pwd,))
result = self.cursor.fetchone()
if not result:
try:
INSERT_SQL = "INSERT INTO scrapyh_pwd (pwd) VALUES (%s);"
self.cursor.execute(INSERT_SQL, (_pwd,))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
def update_pwd(self, _pwd):
UPDATE_SQL = "UPDATE scrapyh_pwd sp SET sp.times = sp.times+1 WHERE sp.pwd = %s;"
self.cursor.execute(UPDATE_SQL, (_pwd,))
self.conn.commit()
def get_failed(self, _id_list):
id_list_str = ', '.join(_id_list)
SELECT_SQL = "SELECT `id`, url, unzip_pwd FROM scrapyh s WHERE s.id IN (%s);"
self.cursor.execute(SELECT_SQL, (id_list_str,))
result = self.cursor.fetchall()
if result:
for r in result:
print(r)