爬虫阶段mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Python连接数据库:
import pymysql
class DB():
def __init__(self,database = "kl",user = "root",password = "123456",port = 3306,host = "localhost"):
self.db = pymysql.connect(host = host,port = port, user = user, database = database, password = password,
cursorclass = pymysql.cursors.DictCursor, charset='utf8mb4')
self.cursor = self.db.cursor()
# 为什么使用data这个元组
#
def update(self,sql, data):
try:
self.cursor.execute(sql, data)
self.db.commit()
except:
print("操作失败,请检查sql语句")
def query(self,sql):
try:
self.cursor.execute(sql)
data = self.cursor.fetchall()
return data
except:
print("查询失败,请检查sql语句")
def __del__(self):
self.cursor.close()
self.db.close()

虫阶

1
2
模糊查询库里的表名
select table_name from user_tables where table_name like '%tab_name%';

cursor.executemany()

sql = “insert into myTable (created_day,name,count) values(%s,%s,%s) ”

args=[(“2012-08-27”,”name1”,100),(“2012-08-27”,”name1”,200),(“2012-08-27”,”name2”,300)]

cursor.executemany(sql, args)

args为列表格式

思维导图网址:

https://www.processon.com/view/link/5cd42958e4b09a3e45bd6a52