# -*- coding: utf-8 -*- from PyQt5.Qt import (QThread, QMoveEvent, QResizeEvent, QPaintEvent, QCloseEvent) from PyQt5.QtCore import (QFileInfo, QSize, QPoint, QRect, QSize, Qt, QUrl, QObject, pyqtSlot, pyqtSignal) from PyQt5.QtWidgets import (QLabel, QLineEdit, QPushButton, QHBoxLayout, QWidget, QApplication, QMainWindow) import requests import time, re from urllib import parse import winreg class ThreadParse(QThread): _signal = pyqtSignal(str, str) def __init__(self, shopid='', sellnum=0): super().__init__() self._shopid = shopid self._sellnum = sellnum def run(self): for i in range(0, 100): ismore = self.worker(self._shopid, self._sellnum ,i) if not ismore: break def worker(self, encrypt_shop_id, sell_num=2, page=1): ismore = True headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0'} cookies = {'sessionid': ''} url = 'https://ec.snssdk.com/productcategory/getShopList?shop_id=%s&type=5&sort=0&size=50&page=%s&b_type_new=0&device_id=0&is_outside=1' % (encrypt_shop_id, page) res = requests.get(url, headers=headers, cookies=cookies).json() if res['st'] == 0: if len(res['data']['list']) > 0: for item in res['data']['list']: sellNum = item['sell_num'] sellNum = re.sub("D", "", sellNum) if int(sellNum) > int(sell_num): self._signal.emit(item['product_id'], 'run') else: self._signal.emit('0', 'success') ismore = False return ismore class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle('抖店商品链接导出') self.createStatusBar() self.createLayouts() self.resize(500, 100) self.root = self.get_desktop() def get_desktop(self): key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders') return winreg.QueryValueEx(key, "Desktop")[0] def shopid(self, url): try: result = parse.urlparse(url) query_dict = parse.parse_qs(result.query) return query_dict['id'][0] except Exception as e: return 'error' def btnStart_click(self): self.shop_id = self.shopid(self.urlLabel.text()) self.sell_num = self.numLabel.text() if self.shop_id == 'error': self.statusBar().showMessage(" 网址解析错误") else: self.btnStart.setText('正在保存..') self.btnStart.setEnabled(False) self.tdParse = ThreadParse(self.shop_id, self.sell_num) self.tdParse._signal.connect(self._writeCSV) self.tdParse.start() def _writeCSV(self, product_id, msg): if msg == 'success': self.btnStart.setText('开始') self.btnStart.setEnabled(True) self.statusBar().showMessage(" 完成,文件已保存在桌面") else: shopdetail = 'https://haohuo.jinritemai.com/views/product/detail?id=%s' % product_id with open(self.root + ('/%s.csv' % self.shop_id), 'a+', encoding='utf-8') as f: f.write(' '+shopdetail) self.statusBar().showMessage(" 正在保存商品 %s" % product_id) def createStatusBar(self): self.statusBar().showMessage(" 输入店铺网址, 销量大于几件 > 开始") def createLayouts(self): self.urlLabel = QLineEdit() self.urlLabel.setPlaceholderText('店铺网址') self.numLabel = QLineEdit() self.numLabel.setFixedSize(40, 20) self.numLabel.setText('2') self.btnStart = QPushButton('开始') self.btnStart.clicked.connect(self.btnStart_click) self.mainLayout = QHBoxLayout() self.mainLayout.addWidget(self.urlLabel) self.mainLayout.addWidget(self.numLabel) self.mainLayout.addWidget(self.btnStart) self.mainWidget = QWidget() self.mainWidget.setLayout(self.mainLayout) self.setCentralWidget(self.mainWidget) if __name__ == '__main__': import sys app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_())