class Doing_Database(object): def __init__(self): # 实例化一个数据库操作对象 self.doing_mysql = Doing_mysql() def do_database(self): print '=====数据分析子功能List:=====\n\n' \ '1、导出词条的相关数据的Excel文件\n' \ '2、二次开发接口(自定义SQL语句)\n' \ '3、数据栏目筛选展示\n' \ '4、返回上层菜单' while True: order = raw_input('=====请输入要执行的子功能序号:======\n').strip() # 去除前后空格 if not order.isdigit(): # 非法数据,重新输入 print '*****请输入合法序号!*****' continue order = int(order) if order == 1: self.set_entry() # 执行导出功能 self.do_export() elif order == 2: self.set_entry() # 执行自定义SQL操作 self.do_DIY_sql() elif order == 3: self.set_entry() # 执行数据库栏目title筛选 self.do_select_title() elif order == 4: # 本层退出,返回home self.doing_mysql.do_end_sql() break else: # 非法数据,重新输入 print '*****请输入合法序号!*****' continue # 设置词条名,即对数据表进行选择 def set_entry(self): try: while True: self.entry = raw_input('请输入要操作的根词条(即要操作的数据表名称):\n') if self.doing_mysql.do_check_is_in(self.entry): # 返回真,证明不存在这个词条 print '本词条不存在' continue else: # 返回假,证明存在,跳出 break except Exception, e: print e
def __init__(self, parent): super(GUI_DATA, self).__init__(parent, title="数据分析系统", size=(1200, 800)) self.parent = parent self.doing_mysql = Doing_mysql() self.Bind(wx.EVT_CLOSE, self.frameClose) # 对系统进行监听关闭键 self.InitUI() self.Centre() self.Show()
def get_user_baike_info(self): try: # 转到个人百科页面 self.driver.get('https://baike.baidu.com/usercenter') time.sleep(2) # 创建个人数据表,以username为table名称来 doing_mysql = Doing_mysql() doing_mysql.do_create_info_table(self.username) # 摘取个人信息 user_pic_url = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[1]/div[1]/img').get_attribute('src') # 头像图片链接 user_level = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[1]/div[4]/i[1]').text # 用户百科等级 tongguo = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[2]/ul/li[1]/a/dl/dd').text # 通过的词条数目 youzhi = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[2]/ul/li[2]/a/dl/dd').text # 优质词条数目 tese = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[2]/ul/li[3]/a/dl/dd').text # 特色词条数目 tijiao = self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div[2]/ul/li[4]/i').text # 提交的词条数目 tongguolv = self.driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div[2]/ul/li[6]/i').text # 通过率 chuangjian = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[2]/ul/li[8]/i').text # 创建版本 caifuzhi = self.driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div[2]/div/div/label/i').text # 财富值 # 将信息写入数据库 doing_mysql.do_add_userinfo(self.username, user_pic_url, user_level, tongguo, youzhi, tese, tijiao, tongguolv, chuangjian, caifuzhi) print self.username,'用户的个人信息已保存至baike数据库的',self.username,'表中。' # 关闭数据库 doing_mysql.do_end_sql() except Exception, e: print '获取并存储个人信息时发生错误:', e
def login(self,event): # 原线程 while True: username = self.username_blank.GetLineText(0) psw = self.psw_blank.GetLineText(0) print username, psw # 实例化Auto_login_baidu类 auto_login = Doing_Auto_login(self.parent.driver, username, psw) if auto_login.login(): # 获得TRUE表示登录成功,可以执行爬取信息操作 # 后续爬取个人信息未完成 if auto_login.get_user_baike_info(): # 爬取成功,弹框提示 dlg_1 = wx.MessageDialog(None, '登录成功。个人百科信息爬取成功,是否立即导出Excel文件到本机?', '爬取成功', wx.YES_NO) if dlg_1.ShowModal() == wx.ID_YES: # 用户要求立即导出Excel文件,执行导出操作。 doing_mysql = Doing_mysql() if doing_mysql.do_ecport2excel(auto_login.get_html_username()): # 导出成功 dlg_1_1 = wx.MessageDialog(None, '导出成功,请前往桌面查看!', '导出Excel成功', wx.OK) if dlg_1_1.ShowModal() == wx.ID_OK: dlg_1_1.Destroy() doing_mysql.do_end_sql() break # 成功的完成所有操作,退出至登录界面 else: # 导出失败 dlg_1_2 = wx.MessageDialog(None, '导出失败!', '错误', wx.OK) if dlg_1_2.ShowModal() == wx.ID_OK: dlg_1_2.Destroy() doing_mysql.do_end_sql() # 导出失败暂时不加循环了,心累!!!! break else: # 用户暂不希望导出,弹框关闭 dlg_1.Destroy() break # 退出循环 else: # 爬取失败 dlg_2 = wx.MessageDialog(None, '登录成功。个人信息爬取失败,请重新登录并爬取。', '爬取失败', wx.OK) if dlg_2.ShowModal() == wx.ID_OK: # 尝试再次爬取 print '重新爬取' dlg_2.Destroy() else: # 登录失败,确认账号密码重新登录 dlg = wx.MessageDialog(None, '登陆失败,请核对用户名和密码!', '登录失败', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() self.username_blank.Clear() self.psw_blank.Clear() break event.Skip()
def output_mysql(self): # 数据刷新到数据库 doing_mysql = Doing_mysql() # 创建该词条的表 data = self.datas[0] data = data['title'].encode('utf-8') doing_mysql.do_create_entry_table(data) for data2 in self.datas: doing_mysql.do_add_entrydata( data, data2['title'].encode('utf-8'), data2['url'], data2['summary'].encode('utf-8').replace("\n", "")) # 关闭SQL doing_mysql.do_end_sql()
def output_mysql(self): # 数据刷新到数据库 doing_mysql = Doing_mysql() try: # 创建该词条的表 data = self.datas[0] data = data['title'].replace(' ', '') doing_mysql.do_create_entry_table(data) for data2 in self.datas: doing_mysql.do_add_entrydata( data, data2['title'], data2['url'], data2['summary'].replace("\n", "")) dlg = wx.MessageDialog( None, '词条信息爬取完毕,并保存至数据库 ' + data.encode("utf-8") + ' 数据表中。', '爬取成功!', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() except Exception, e: print e
class GUI_DATA(wx.Frame): def __init__(self, parent): super(GUI_DATA, self).__init__(parent, title="数据分析系统", size=(1200, 800)) self.parent = parent self.doing_mysql = Doing_mysql() self.Bind(wx.EVT_CLOSE, self.frameClose) # 对系统进行监听关闭键 self.InitUI() self.Centre() self.Show() def InitUI(self): panel = wx.Panel(self) # 最外层盒子,垂直方向 self.boxsizer = wx.BoxSizer(wx.VERTICAL) # 第1个内层盒子,内建两个盒子,一左一右 hbox1 = wx.BoxSizer(wx.HORIZONTAL) # 左侧细长的盒子,垂直 hbox1_1 = wx.BoxSizer(wx.VERTICAL) # ==============左侧第三个功能========= hbox1_1_3 = wx.BoxSizer(wx.HORIZONTAL) static_str3 = wx.StaticText(panel, label='预览词条文件:') hbox1_1_3.Add(static_str3, flag=wx.ALIGN_CENTER_VERTICAL) self.entry_view_blank = wx.TextCtrl(panel) # , size=(200,20)) hbox1_1_3.Add(self.entry_view_blank, flag=wx.ALIGN_CENTER_VERTICAL) hbox1_1.Add(hbox1_1_3, flag=wx.ALL, border=15) btn_view = wx.Button(panel, label='获取预览信息', size=(120, 30)) btn_view.Bind(wx.EVT_LEFT_DOWN, self.getview) # 监听事件,获取预览信息 hbox1_1.Add(btn_view, flag=wx.ALIGN_CENTER_HORIZONTAL | wx.EXPAND | wx.LEFT | wx.RIGHT, border=15) hbox1_1.Add((-1, 5)) btn_empty = wx.Button(panel, label='清空预览信息', size=(120, 30)) btn_empty.Bind(wx.EVT_LEFT_DOWN, self.emptyview) # 监听事件,清空预览信息 hbox1_1.Add(btn_empty, flag=wx.ALIGN_CENTER_HORIZONTAL | wx.EXPAND | wx.LEFT | wx.RIGHT, border=15) hbox1_1.Add((-1, 25)) # =============左侧第一个功能========== hbox1_1_1 = wx.BoxSizer(wx.HORIZONTAL) static_str1 = wx.StaticText(panel, label='要导出Excel文件的根词条:') hbox1_1_1.Add(static_str1, flag=wx.ALIGN_CENTER_VERTICAL) self.entry_blank = wx.TextCtrl(panel) #, size=(200,20)) hbox1_1_1.Add(self.entry_blank, flag=wx.ALIGN_CENTER_VERTICAL) hbox1_1.Add(hbox1_1_1, flag=wx.ALL, border=15) btn_export = wx.Button(panel, label='导出Excel', size=(120, 30)) btn_export.Bind(wx.EVT_LEFT_DOWN, self.export2Excel) # 监听事件,导出Excel hbox1_1.Add(btn_export, flag=wx.ALIGN_CENTER_HORIZONTAL | wx.EXPAND | wx.LEFT | wx.RIGHT, border=15) hbox1_1.Add((-1, 25)) # ==============左侧第二个功能========== hbox1_1_2 = wx.BoxSizer(wx.VERTICAL) static_str2 = wx.StaticText(panel, label='【开发者选项】 慎选!\n请按语法规定输入SQL语句:') hbox1_1_2.Add(static_str2, flag=wx.EXPAND | wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL) self.sql_blank = wx.TextCtrl(panel, style=wx.TE_MULTILINE, size=(240, 100)) # , size=(200,20)) hbox1_1_2.Add(self.sql_blank, flag=wx.EXPAND | wx.TOP, border=15) hbox1_1.Add(hbox1_1_2, flag=wx.ALL, border=15) btn_dosql = wx.Button(panel, label='执行SQL语句', size=(120, 30)) btn_dosql.Bind(wx.EVT_LEFT_DOWN, self.doSQL) # 监听事件,执行SQL语句 hbox1_1.Add(btn_dosql, flag=wx.ALIGN_CENTER_HORIZONTAL | wx.EXPAND | wx.LEFT | wx.RIGHT, border=15) # ===============右侧大表============== hbox1.Add(hbox1_1, proportion=2) # 右侧宽大的,直接用GridSizer放进Box hbox1_2 = wx.BoxSizer(wx.HORIZONTAL) self.grid = wx.grid.Grid(panel) self.grid.CreateGrid(100, 10) # 100行10列 # self.grid.SetCellValue(0,0,'hello') hbox1_2.Add(self.grid, flag=wx.ALIGN_RIGHT, border=35) hbox1.Add(hbox1_2, proportion=2) # 最外层盒子添加内层1号盒子 self.boxsizer.Add(hbox1, flag=wx.TOP | wx.ALIGN_CENTER_VERTICAL, proportion=15, border=10) # 下边距10 self.boxsizer.Add((-1, 10)) # ==============底部按钮============== # 创建内层3号盒子,内含两个按钮,水平放置 self.hbox3 = wx.BoxSizer(wx.HORIZONTAL) # 按钮大小,名称设置 btn_back = wx.Button(panel, label='返回上层菜单', size=(120, 30)) btn_back.Bind(wx.EVT_LEFT_DOWN, self.back2home) self.hbox3.Add(btn_back) btn_exit = wx.Button(panel, label='退出系统', size=(120, 30)) self.hbox3.Add(btn_exit, flag=wx.LEFT | wx.BOTTOM, border=5) # 按钮距离左侧和底部都是5 btn_exit.Bind(wx.EVT_LEFT_DOWN, self.closeFrame) # 最外层盒子添加内层4号盒子,盒子右对齐,靠在外层盒子的右侧,边距10 self.boxsizer.Add(self.hbox3, flag=wx.ALIGN_RIGHT | wx.RIGHT, border=10) panel.SetSizer(self.boxsizer) def back2home(self, event): self.doing_mysql.do_end_sql() self.parent.Show() self.Destroy() # self.Hide() event.Skip() def frameClose(self, event): # 监听系统的关闭键 self.parent.Show() self.doing_mysql.do_end_sql() event.Skip() def export2Excel(self, event): # 导出Excel文件 tablename = self.entry_blank.GetLineText(0) if self.doing_mysql.do_check_is_in(tablename): # 返回真证明不存在, 弹框提示 dlg = wx.MessageDialog( None, tablename.encode('utf-8') + '数据表不存在,请核对或导出其他词条文件!', '词条文件不存在!', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() else: # 返回假证明存在, 执行导出操作 if self.doing_mysql.do_ecport2excel(tablename): # 导出成功 dlg = wx.MessageDialog( None, tablename.encode('utf-8') + '.xlsx文件导出成功,请前往桌面查看!', '导出成功!', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() self.entry_blank.Clear() else: # 导出失败 dlg = wx.MessageDialog( None, tablename.encode('utf-8') + '.xlsx文件导出失败,请重新尝试导出!', '导出失败!', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() event.Skip() def getview(self, event): entry = self.entry_view_blank.GetLineText(0) if self.doing_mysql.do_check_is_in(entry): # 返回真证明不存在, 弹框提示 dlg = wx.MessageDialog( None, entry.encode('utf-8') + '数据表不存在,请核对或导出其他词条文件!', '词条文件不存在!', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() else: self.doing_mysql.do_getdata2grid(entry, self.grid) self.grid.Update() event.Skip() def emptyview(self, event): self.grid.ClearGrid() self.entry_view_blank.Clear() event.Skip() def doSQL(self, event): # 执行SQL语句 self.grid.ClearGrid() sql = self.sql_blank.GetValue() # print sql if self.doing_mysql.do_DIYsql2grid(sql, self.grid): pass else: dlg = wx.MessageDialog(None, 'SQL执行失败,请仔细核对是否为语法问题!', '执行失败!', wx.OK) if dlg.ShowModal() == wx.ID_OK: dlg.Destroy() event.Skip() def closeFrame(self, event): # 关闭整个系统 self.doing_mysql.do_end_sql() self.parent.Close() self.Destroy() event.Skip()
def __init__(self): # 实例化一个数据库操作对象 self.doing_mysql = Doing_mysql()