python 爬蟲程式 -2 加入interface視窗介面

加入pythin的interface視窗介面

這裡要使用tkinter函式庫
  1. import tkinter as tk
  2. # button 被按下觸發的事件
  3. def clickon():
  4. outputtext['text'] = inputtext.get() + '\n' + inputtext.get() #將文字方塊的文字填入outputtext的text裡面
  5.  
  6. win=tk.Tk()
  7. win.title("My First Tk GUI") # window title name
  8.  
  9. label = tk.Label(win, text="Hello World!") #建立標籤物件
  10. label.grid(row=0, column=0) # layout
  11.  
  12. inputtext = tk.Entry(win) #建立填入文字方塊
  13. inputtext.grid(row=0, column=1, columnspan=20)
  14.  
  15. button = tk.Button(win)#, text="OK" #建立 button
  16. button['text'] = 'ok'
  17. button['command'] = clickon #button event事件
  18. button.grid(row=1, column=1)
  19. outputtext = tk.Label(win, text = "output information")
  20. outputtext.grid(row = 2, column = 1)
  21. win.mainloop()

GUI test
建立的物件的屬性有兩種寫法
#第一種
button = tk.Button(win)#, text="OK"             #建立 button 
button['text'] = 'ok'
button['command'] = clickon                     #button event事件

#第二種
button = tk.Button(win, text = "OK", command = clickon)             #建立 button 

一次建立多個物件

用陣列儲存
在物件屬性可以設定
  • text文字:string格式
  • height高度, width寬度:只能輸入int的數字
  • anchor(靠左、靠右、至中) = "n", "ne", "e", "se", "s", "sw", "w", "nw", and also "center".
  • variable值

在layout可以設定
  • row, column
  • sticky(對齊) = "N","W","S","E"


  1. all_headlist = ['職務名稱','公司名稱','學歷', '地區', '應徵人數']
  2. default_check = [1,1,0,0,1] #預設checkbutton的值
  3. checktext = {}
  4. checkb={}
  5. var = {}
  6.  
  7. for i in range(0, len(all_headlist)):
  8.  
  9. var[i] = tk.IntVar(value = default_check[i])
  10. checkb[i] = tk.Checkbutton(win, text = all_headlist[i]+':', height = 2, anchor = 'nw', variable = var[i])
  11. checkb[i].grid(row = i+4, column = 0, sticky = "W")
  12. checktext[i] = tk.Label(win, width = 50, anchor = 'sw')
  13. checktext[i].grid(row = i+4, column = 1, sticky = "W")

GUI checkbutton

讀檔方式

當讀取單一直行,以下是只顯示每一列的'職務名稱'跟'學歷'
with open('./'+filename + filetyoe,'r') as oldfile:
    reader = csv.DictReader(oldfile)
    for rows in reader:
        print(rows['職務名稱'], rows['學歷'])
    oldfile.close()

完整程式碼

功能說明: 輸入要匯出的檔案名稱,以及搜尋的資料,按下search,等待資料抓取完成,會存入你所設定的名稱 按下output my choose,匯出所選擇的資訊,存入你設定得名稱+_choose 按下load file to choose,讀取你設定的名稱的檔案,選擇完要匯出的資訊後再存入你設定得名稱+_choose
  1. import tkinter as tk
  2. import tkinter as tk
  3. import csv
  4. import time
  5. import load_104_newpeopleV1_0
  6.  
  7. test = False
  8.  
  9. # search_url =
  10. #https://www.104.com.tw/area/freshman/search?keyword=%E6%A9%9F%E5%99%A8%E4%BA%BA&area=6001001000,6001002000&jobcategory=2007000000&industry=&page=1&sortField=APPEAR_DATE&sortMode=DESC
  11. all_headlist = ['職務名稱','公司名稱','學歷', '地區', '應徵人數',
  12. '工作說明', '職務類別', '工作待遇', '工作性質', '上班地點', '管理責任', '出差外派', '上班時段', '休假制度', '可上班日', '需求人數', '接受身份', '工作經歷', '學歷要求', '科系要求', '語文條件', '擅長工具', '工作技能', '具備駕照', '其他條件', '公司福利',
  13. '產業類別', '產業描述', '員工', '資本額', '聯絡人', '公司地址', '電話', '傳真', '公司網址', '公司簡介', '主要商品/服務項目']
  14. checktext = {}
  15. filename = '104_output'
  16. filetyoe = '.csv'
  17. checkb={}
  18. newfile_title = []
  19. default_check = [1,1,1,1,1, #5
  20. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0, #21
  21. 1,1,1,1,1,1,0,0,1,0,1] #11
  22.  
  23. # delete " with show information
  24. def deltwocom(text, number):
  25. tmp = list(text)
  26. tmp[number] = ''
  27. text = ''.join(tmp)
  28. return text
  29.  
  30. # load total file and show information
  31. def load_example(filename):
  32. with open('./'+filename + filetyoe,'r') as csvfile:
  33.  
  34. title = csvfile.readline().split('","')
  35. info = csvfile.readline().split('","')
  36. title[0] = deltwocom(title[0], 0)
  37. title[36] = deltwocom(title[36], len(title[36])-2)
  38. info[0] = deltwocom(info[0], 0)
  39. info[36] = deltwocom(info[36], len(info[36])-2)
  40.  
  41. for i in range(37):
  42. overhead = 25 #限制最大顯示長度
  43. if len(info[i]) >= overhead:
  44. checktext[i]['text'] =info[i][0:overhead] + '...'
  45. else:
  46. checktext[i]['text'] =info[i] + ''
  47. csvfile.close()
  48. #output _choose file
  49. def creatfile(filename):
  50. with open('./'+filename + filetyoe,'r') as oldfile:
  51. reader = csv.DictReader(oldfile)
  52. with open('./'+filename+'_choose' + filetyoe, 'w') as newfile:
  53. # first line title
  54. wl = '"' + all_headlist[newfile_title[0]] + '"'
  55. for i in range(1, len(newfile_title)):
  56. wl += ',"' + all_headlist[newfile_title[i]] + '"'
  57. newfile.writelines(wl + '\n')
  58. # data
  59. for rows in reader:
  60. print(rows['職務名稱'], rows['學歷'])
  61. wl = '"' + rows[all_headlist[newfile_title[0]]] + '"'
  62. for i in range(1, len(newfile_title)):
  63. wl += ',"' + rows[all_headlist[newfile_title[i]]] + '"'
  64. newfile.writelines(wl + '\n')
  65. newfile.close
  66. oldfile.close()
  67.  
  68. #(button) search url and output total file
  69. def clickto_search():
  70. url = inputurl.get()
  71. outputtext['text'] = '抓取資料中...'
  72. time.sleep(1)
  73. res = load_104_newpeopleV1_0.load_104_newpeople_main(url, './' + filename + filetyoe, test = test)
  74. outputtext['text'] = '完成' + res + '\toutput file name is ' + filename + filetyoe
  75. load_example(filename)
  76.  
  77. #(button) load file to choose
  78. def load_to_choose():
  79. filename = input_fname.get()
  80. outputtext['text'] = '載入 '+ filename + filetyoe +' 資料'
  81. load_example(filename)
  82. outputtext['text'] = '載入 '+ filename + filetyoe +' 資料完成'
  83. #(button) output my choose to file
  84. def output_choose():
  85. filename = input_fname.get()
  86. outputtext['text'] = '存入 '+ filename + filetyoe +'_choose 中...'
  87. for i in range(len(all_headlist)):
  88. #print(var[i].get())
  89. if var[i].get():
  90. newfile_title.append(i)
  91. creatfile(filename)
  92. outputtext['text'] = filename +'_choose' + filetyoe + '儲存完成'
  93.  
  94. win=tk.Tk()
  95. win.title("load_104_newpeople_job")
  96.  
  97. label_fname = tk.Label(win, text="匯出的檔案名稱: ") #建立標籤物件
  98. label_fname.grid(row=0, column=0)
  99.  
  100. v1 = tk.StringVar(win, value='104_output') #預設文字方塊輸入值
  101. input_fname = tk.Entry(win, width = 100, textvariable=v1)
  102. input_fname.grid(row=0, column=1, columnspan=101, sticky = "W")
  103.  
  104. labelurl = tk.Label(win, text="輸入 search url : ") #建立標籤物件
  105. labelurl.grid(row=1, column=0)
  106.  
  107. v2 = tk.StringVar(win, value='https://www.104.com.tw/area/freshman/search?keyword=%E6%A9%9F%E5%99%A8%E4%BA%BA&area=6001001000,6001002000&jobcategory=2007000000&industry=&page=1&sortField=APPEAR_DATE&sortMode=DESC')
  108. inputurl = tk.Entry(win, width = 100, textvariable=v2)
  109. inputurl.grid(row=1, column=1, columnspan=101, sticky = "W")
  110.  
  111. button = tk.Button(win)
  112. button['text'] = 'search'
  113. button['command'] = clickto_search
  114. button.grid(row=2, column=0)
  115.  
  116. buttontest = tk.Button(win)
  117. buttontest['text'] = 'load file to choose'
  118. buttontest['command'] = load_to_choose
  119. buttontest.grid(row=2, column=1)
  120.  
  121. buttonget = tk.Button(win)
  122. buttonget['text'] = 'output my choose'
  123. buttonget['command'] = output_choose
  124. buttonget.grid(row=2, column=2)
  125. outputtitle = tk.Label(win, text = "工作狀態")
  126. outputtitle.grid(row = 0, column = 5)
  127. outputtext = tk.Label(win, text = "output information")
  128. outputtext.grid(row = 1, column = 5)
  129.  
  130. var = {}
  131.  
  132. for i in range(0, len(all_headlist)):
  133. if i >= 26:
  134. row = i-26+3
  135. col = 4
  136. elif i <= 4:
  137. row = i+3
  138. col = 0
  139. else:
  140. row = i-5+3
  141. col = 2
  142. var[i] = tk.IntVar(value = default_check[i])
  143. checkb[i] = tk.Checkbutton(win, text = all_headlist[i]+':', height = 2, anchor = 'nw', variable = var[i]) #
  144. checkb[i].grid(row = row, column = 0 + col, sticky = "W")
  145. checktext[i] = tk.Label(win, width = 50, anchor = 'sw')
  146. checktext[i].grid(row = row, column = 1+col, sticky = "W")
  147. win.mainloop()

GUI

留言

這個網誌中的熱門文章

C# 模擬鍵盤滑鼠控制電腦

android 定時通知(永久長期的) 本篇只講AlarmManager使用

python nn 聲音辨識 -1 傅立葉轉換

python pyautogui 簡介

raspberrypi 開機自動執行程式 與 在terminal開啟第二個terminal執行python