import pandas as pd import pymysql from sqlalchemy import create_engine class Service2DB: """ +--------------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | host | varchar(255) | YES | | NULL | | | resource_url | text | YES | | NULL | | | resource_type | int(11) | NO | | NULL | | | is_third_party | int(11) | YES | | NULL | | | parent | text | NO | | NULL | | | is_eval_node | int(11) | NO | | NULL | | | node_creation | int(11) | NO | | NULL | | | node_insertion | int(11) | NO | | NULL | | | node_removal | int(11) | NO | | NULL | | | node_attach_later | int(11) | NO | | NULL | | | attr_addition | int(11) | NO | | NULL | | | attr_modification | int(11) | NO | | NULL | | | attr_removal | int(11) | NO | | NULL | | | attr_style_text_addition | int(11) | NO | | NULL | | | attr_style_removal | int(11) | NO | | NULL | | | net_script_req | int(11) | NO | | NULL | | | net_image_req | int(11) | NO | | NULL | | | net_iframe_req | int(11) | NO | | NULL | | | net_xmlhttp_req | int(11) | NO | | NULL | | | net_Link_req | int(11) | NO | | NULL | | | net_video_req | int(11) | NO | | NULL | | | score | float | NO | | NULL | | | category | varchar(255) | NO | | NULL | | | website | varchar(255) | NO | | NULL | | +--------------------------+--------------+------+-----+---------+----------------+ """ def __init__(self): self.user = "root" self.host = "localhost" self.passwd = "121xiaoshitou121" self.database = "serviceDependency" self.table = "shandong" self.engine = self.connect() self.dat = { "host": [], "resource_url": [], "resource_type": [], "is_third_party": [], "parent": [], "is_eval_node": [], "node_creation": [], "node_insertion": [], "node_removal": [], "node_attach_later": [], "attr_addition": [], "attr_modification": [], "attr_removal": [], "attr_style_text_addition": [], "attr_style_removal": [], "net_script_req": [], "net_image_req": [], "net_iframe_req": [], "net_xmlhttp_req": [], "net_Link_req": [], "net_video_req": [], "score": [], "category": [], "website": [], } def connect(self): return create_engine('mysql+pymysql://{}:{}@{}:3306/{}' .format(self.user, self.passwd, self.host, self.database)) def readDB(self, sql=None): sql = "select * from {}".format(self.table) df_read = pd.read_sql_query(sql, self.engine) return df_read def writeDB(self, data): df_write = pd.DataFrame(data) df_write.to_sql(self.table, self.engine, index=False, if_exists="append")