Guest User

main_controller.py

a guest
Dec 27th, 2022
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. from PyQt5.QtWidgets import QMainWindow
  2. import threading
  3.  
  4. from scrape_ui import Ui_MainWindow
  5. from scrape_shopee_controller import scrape_shopee
  6. from scrape_ruten_controller import scrape_ruten
  7.  
  8. class controller(QMainWindow):
  9. def __init__(self):
  10. super().__init__()
  11. self.ui = Ui_MainWindow()
  12. self.ui.setupUi(self)
  13. self.setup_control()
  14.  
  15. def setup_control(self):
  16. self.ui.start_button.clicked.connect(self.onButtonClick)
  17.  
  18. def onButtonClick(self): # 按下開始按鈕
  19. self.product_keyword = self.ui.keyword_input.text() # 獲取商品關鍵字
  20. self.product_pages = self.ui.pages_input.text() # 獲取要爬取的頁數
  21. print(self.product_keyword, self.product_pages)
  22.  
  23. if self.ui.type_shopee_button.isChecked(): # 按下shopee按鈕
  24. self.display_text('開始執行蝦皮爬蟲')
  25. self.thread_scrape_shopee = threading.Thread(target=self.shopee_control)
  26. self.thread_scrape_shopee.start()
  27.  
  28. if self.ui.type_ruten_button.isChecked(): # 按下ruten按鈕
  29. self.display_text('開始執行露天爬蟲')
  30. self.thread_scrape_ruten = threading.Thread(target=self.ruten_control)
  31. self.thread_scrape_ruten.start()
  32.  
  33. def display_text(self, text): # 把接收到的text文字顯示在state_table(textBrowser)上
  34. print(f'{text}')
  35. self.ui.state_table.append(f'{text}')
  36.  
  37. def shopee_control(self): # 執行scrape_shopee
  38. self.scrape_shopee = scrape_shopee(self.product_keyword, self.product_pages)
  39.  
  40. self.display_text('獲取商品關鍵字連結')
  41. self.scrape_shopee.get_url() # 獲取所要爬的關鍵字連結
  42.  
  43. self.display_text('獲取商品資訊')
  44. dt_all = [] # 用來存取商品資訊
  45. for i in range(0, len(self.scrape_shopee.urls)):
  46. scrapes = self.scrape_shopee.scrape(self.scrape_shopee.urls[i])
  47. dt_all.extend(scrapes)
  48.  
  49. self.display_text('建立商品資訊表格')
  50. self.scrape_shopee.data_frame(dt_all) # 建一個dataframe
  51.  
  52. dataframe_name = self.scrape_shopee.save_to_xlsx() # 把dataframe存成excel檔
  53. self.display_text(f'儲存完畢,檔案名稱為"{dataframe_name}"')
  54.  
  55. def ruten_control(self): # 執行scrape_ruten
  56. self.scrape_ruten = scrape_ruten(self.product_keyword, self.product_pages)
  57.  
  58. self.display_text('獲取商品關鍵字連結')
  59. self.scrape_ruten.get_url() # 獲取所要爬的關鍵字連結
  60.  
  61. self.display_text('獲取商品資訊')
  62. dt_all = [] # 用來存取商品資訊
  63. for i in range(0, len(self.scrape_ruten.urls)):
  64. scrapes = self.scrape_ruten.scrape(self.scrape_ruten.urls[i])
  65. dt_all.extend(scrapes)
  66.  
  67. self.display_text('建立商品資訊表格')
  68. self.scrape_ruten.data_frame(dt_all) # 建一個dataframe
  69.  
  70. dataframe_name = self.scrape_ruten.save_to_xlsx() # 把dataframe存成excel檔
  71. self.display_text(f'儲存完畢,檔案名稱為"{dataframe_name}"')
Add Comment
Please, Sign In to add comment