電商平臺數據爬蟲+GUI可視化界面

電商平臺數據抓包軟件由三部分組成:前端GUI可視化界面+後端連接+數據庫

1、GUI可視化界面:

  • 結果展示:

在這裏插入圖片描述

- 代碼:

  def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):  # 設計界面
        self.setGeometry(300, 200, 1280, 350)  # 設置窗體尺寸
        palette = QPalette()
        palette.setBrush(QPalette.Background, QBrush(QPixmap("./0101.jpg")))
        self.setPalette(palette)
        self.setWindowTitle('電商收集平臺')  # 命名窗體的標題
        QToolTip.setFont(QFont('SansSerif', 12))  # 設置控件提示信息的字體格式及大小
        self.btn = QPushButton('開始', self)  # 設計開始按鈕
        self.btn.setToolTip('單擊開始爬取')  # 設置按鈕的提示信息
        self.btn.clicked.connect(self.doAction)  # 建立信號和槽的聯繫,將單擊信號與下面的doacion進行鏈接
        #self.pbar = QProgressBar(self)  # 設計一個進度條
        self.textEdit = QTextEdit(self)  # 設計一個文本輸出框
        self.textEdit.setPlaceholderText("幫助文檔:\n1.右側輸入商品名稱\n2.單擊開始進行下載\n3.單擊停止結束下載")
        self.textEdit.setStyleSheet("color:black")
        self.textEdit.resize(1150, 200)  # 設置文本輸出框大小
        self.lineEdit1 = QLineEdit(self)  # 設計輸入框
        self.lineEdit1.setPlaceholderText("輸入搜索關鍵字")
        self.lineEdit2 = QLineEdit(self)  # 設計輸入框
        self.lineEdit2.setPlaceholderText("輸入爬取數據量")

        self.groupBox = QGroupBox(self)  # 設置QT容器
        self.groupBox.move(1130, 125)  # 移動容器的位置
        self.groupBox.resize(120, 190)  # 設置容器的尺寸
        self.groupBox.setTitle('菜單欄')  # 設置容器的標題
        self.groupBox.setStyleSheet("color:black")
        self.groupBox.setFont(QFont("微軟雅黑", 14, QFont.Bold))
        self.groupBox.setAlignment(4)  # 4爲ALignHCenter爲居中的意思
        layout = QVBoxLayout()  # 新建一個垂直佈局
        layout.addWidget(self.lineEdit1)  # 往該佈局中添加各種控件
        layout.addWidget(self.lineEdit2)  # 往該佈局中添加各種控件
        layout.addWidget(self.btn)
        self.groupBox.setLayout(layout)  # 顯示該佈局

        self.groupBox2 = QGroupBox(self)  # 設置QT容器2
        self.groupBox2.move(10, 10)
        self.groupBox2.resize(1110, 300)
        self.groupBox2.setTitle('程序可視化界面')
        self.groupBox2.setStyleSheet("color:white")
        self.groupBox2.setFont(QFont("微軟雅黑",14, QFont.Bold))
        self.groupBox2.setAlignment(4)  # 4爲ALignHCenter爲居中的意思
        layout2 = QVBoxLayout()
        layout2.addWidget(self.textEdit)
        self.groupBox2.setLayout(layout2)

        self.groupBox5 = QGroupBox(self)  # 設置QT容器5
        self.groupBox5.setStyleSheet("border:none")  # 隱藏容器的邊框
        self.groupBox5.move(1128, 8)
        self.groupBox5.resize(125, 120)
        self.lbl = QLabel(self)  # 新建一個控件來顯示圖片
        layout5 = QVBoxLayout()
        pixmap = QPixmap("./011.jpg")  # 按指定路徑找到圖片
        self.lbl.setPixmap(pixmap)  # 在label上顯示圖片
        self.lbl.setScaledContents(True)  # 讓圖片自適應label大小
        layout5.addWidget(self.lbl)
        self.groupBox5.setLayout(layout5)
        self.show()  # 顯示主窗口



    def doAction(self):  # 主函數,用於下載數據
        goods = []
        name = self.lineEdit1.text()  # 獲取輸入的內容
        number = int(self.lineEdit2.text())  # 獲取輸入的內容
        QMessageBox.question(self, '提示',
                             "單擊yes爬取數據,爬取過程請耐心等待", QMessageBox.Yes, QMessageBox.Yes)  # 最後一個QMessageBox.No的意思是默認爲no
        try:
            browser.get('https://www.jd.com/')
            input = wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR, '#key'))
            )
            submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#search > div > div.form > button > i')))
            input.send_keys(str(name))
            submit.click()
            goods = get_products()
            for good in goods:
                time.sleep(1)
                self.textEdit.append(str(good))
                QApplication.processEvents()  # 刷新窗口防止卡死
        except TimeoutError:
            print('error')
        for i in range(2, int(number/60)+3):
            goods = []
            QApplication.processEvents()  # 刷新窗口防止卡死
            time.sleep(random.randint(70, 80))
            goods = next_page(i)
            for good in goods:
                time.sleep(1)
                self.textEdit.append(str(good))
                QApplication.processEvents()  # 刷新窗口防止卡死
            self.textEdit.append('停止下載 ')
        browser.close()


    def closeEvent(self, event):  # 重寫關閉事件
        reply = QMessageBox.question(self, '提示',  # 設計一個提示框
                                     "確定要退出搜索程序嗎?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)  # 最後一個QMessageBox.No的意思是默認爲no
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

2、後臺運行:

  • 結果展示:
    右側輸入搜索的商品,以及想要爬取的數據總量,也可輸入商品的url:
    在這裏插入圖片描述
    後臺運行,將登錄模擬瀏覽器,搜索商品:
    在這裏插入圖片描述
    在這裏插入圖片描述
    將蒐集的商品數據顯示,並錄入數據庫中:
    在這裏插入圖片描述
    此爲後端平臺的爬取過程:❤❤❤
    在這裏插入圖片描述
  • 代碼:
def next_page(page_number):
    try:
        submit = wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_bottomPage > span.p-num > a.pn-next')))
        submit.click()
        Goods = get_products()
        return Goods
    except TimeoutError:
        next_page(page_number)

def get_products():
    Good = []
    wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR,'#J_goodsList .gl-warp .gl-item')))
    html = browser.page_source
    doc = pq(html)
    items = doc('#J_goodsList .gl-warp .gl-item').items()
    for item in items:
        product = {
            'name':item.find('.p-name').text(),
            'shop':item.find('.p-shop').text(),
            'price':item.find('.p-price').text(),
            'commit':item.find('.p-commit').text(),
            'self': item.find('.p-icons .goods-icons').text(),
        }
        print(product)
        Good.append(str(product))
        save_to_mongo(product)
        QApplication.processEvents()  # 刷新窗口防止卡死
        if stop == 1:
            break
    return Good

3、數據庫接口及管理:

  • 結果展示:
    在這裏插入圖片描述
  • 代碼:
MONGO_URL = 'localhost'
MONGO_DB = 'jingdong'
MONGO_TABLE = 'product'


SERVICE_ARGS = ['--load-images=false','--disk-cache=true']
def save_to_mongo(result):
    try:
        if db[MONGO_TABLE].insert(result):
            print('存儲到MONGO_DB成功',result)
    except Exception:
        print('存儲到MONGO_DB失敗',result)

最新出爐的後臺爬蟲+可視化操作,有興趣的請在下方留言交流~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章