forked from starFalll/Spider
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Delete_user.py to delete useless uid
- Loading branch information
Showing
6 changed files
with
62 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
#连接数据库 | ||
from sqlalchemy import create_engine | ||
from weibo.sina_spider import loadconf_db | ||
from yaml import load | ||
|
||
#加载配置 | ||
def loadconf_db(file_path): | ||
with open(file_path,'r',encoding='utf-8') as f: | ||
cont=f.read() | ||
cf=load(cont) | ||
return cf | ||
|
||
def Connect(file): | ||
conf = loadconf_db(file) | ||
db = conf.get('db') | ||
connect_str = 'mysql+pymysql://' + db['user'] + ':' + db['password'] + '@127.0.0.1:3306/weibo?charset=utf8mb4' | ||
connect_str = 'mysql+pymysql://' + str(db['user']) + ':' + str(db['password']) + '@127.0.0.1:3306/weibo?charset=utf8mb4' | ||
engine = create_engine(connect_str, encoding='utf-8') | ||
return engine | ||
return conf,engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#删除不在conf.yaml配置文件中的微博用户及其动态 | ||
from weibo.Connect_mysql import Connect | ||
from sqlalchemy import create_engine, MetaData,Table, select ,delete | ||
|
||
def DeleteUsers(): | ||
conf,engine = Connect('conf.yaml') | ||
conn = engine.connect() | ||
metadata = MetaData(engine) | ||
WBData = Table('WBData', metadata, autoload=True) | ||
WBUser = Table('WBUser', metadata, autoload=True) | ||
empty = select([WBUser.c.uid]) | ||
res = conn.execute(empty)#得到WBUser表中所有的uid | ||
deluid = [] #要删除的uid | ||
uids = conf.get('uids') | ||
uids = list(uids.values())#得到配置文件中的uid | ||
for r in res: | ||
if(int(r[0]) not in uids): | ||
deluid.append(r[0]) | ||
for uid in deluid: | ||
exc = WBData.delete().where(WBUser.c.uid==str(uid)) #删除用户动态信息 | ||
conn.execute(exc) | ||
exc = WBUser.delete().where(WBUser.c.uid==str(uid))#删除用户个人信息 | ||
conn.execute(exc) | ||
|
||
|
||
conn.close() | ||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
DeleteUsers() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters