[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'expressQuery.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
import sys
import requests
import tkinter
import asyncio
from pyppeteer import launch
import time
from bs4 import BeautifulSoup
def screen_size():
tk = tkinter.Tk()
width = tk.winfo_screenwidth()
height = tk.winfo_screenheight()
tk.quit()
return {'width': width, 'height': height}
async def mainProgram3():
global page
global browser
browser = await launch(headless=True, args=['--start-maximized'],
ignoreDefaultArgs=['--enable-automation']) # 页面全屏
page = await browser.newPage()
await page.setViewport(screen_size())
# await page.setCookie({'cookie': 'ALF=1607876296; WEIBOCN_FROM=1110003030; SUB=_2A25yvNZfDeRhGeFO61cR-SnMzDqIHXVuXvoXrDV6PUJbkdANLUPekW1NQYXJAY4sVZJyyWrZmdiOCd7yq2S5YMEs; _T_WM=85956253883; MLOGIN=1; XSRF-TOKEN=dd7c1c; M_WEIBOCN_PARAMS=luicode%3D20000174%26lfid%3D1008088e92193d0e74d876aa8aaee841021081_-_feed%26uicode%3D20000174'}) # 内容全屏
await page.goto('https://www.guoguo-app.com/')
await page.evaluate(
'''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''') # 以下为插入中间js,将淘宝会为了检测浏览器而调用的js修改其结果。
await page.evaluate('''() =>{ window.navigator.chrome = { runtime: {}, }; }''')
await page.evaluate(
'''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
await page.evaluate(
'''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
await page.evaluate('window.scrollTo(0,document.body.scrollHeight)')
asyncio.get_event_loop().run_until_complete(mainProgram3())
class Ui_singlenumberinput(object):
def setupUi(self, singlenumberinput):
singlenumberinput.setObjectName("singlenumberinput")
singlenumberinput.resize(466, 388)
self.CourierCompany = QtWidgets.QLineEdit(singlenumberinput)
self.CourierCompany.setGeometry(QtCore.QRect(160, 49, 131, 20))
self.CourierCompany.setObjectName("CourierCompany")
self.inputNumber = QtWidgets.QLabel(singlenumberinput)
self.inputNumber.setGeometry(QtCore.QRect(90, 49, 60, 23))
self.inputNumber.setAlignment(QtCore.Qt.AlignCenter)
self.inputNumber.setObjectName("inputNumber")
self.ReturnsResult = QtWidgets.QTextBrowser(singlenumberinput)
self.ReturnsResult.setGeometry(QtCore.QRect(50, 84, 361, 295))
self.ReturnsResult.setObjectName("ReturnsResult")
self.widget = QtWidgets.QWidget(singlenumberinput)
self.widget.setGeometry(QtCore.QRect(90, 7, 282, 25))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.singleNumberInput = QtWidgets.QLabel(self.widget)
self.singleNumberInput.setAlignment(QtCore.Qt.AlignCenter)
self.singleNumberInput.setObjectName("singleNumberInput")
self.horizontalLayout.addWidget(self.singleNumberInput)
self.oddip = QtWidgets.QLineEdit(self.widget)
self.oddip.setObjectName("oddip")
self.horizontalLayout.addWidget(self.oddip)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton.clicked.connect(self.run)
self.retranslateUi(singlenumberinput)
QtCore.QMetaObject.connectSlotsByName(singlenumberinput)
def retranslateUi(self, singlenumberinput):
_translate = QtCore.QCoreApplication.translate
singlenumberinput.setWindowTitle(_translate("singlenumberinput", "单号查询小工具"))
self.inputNumber.setText(_translate("singlenumberinput", "快递公司:"))
self.singleNumberInput.setText(_translate("singlenumberinput", "输入单号:"))
self.pushButton.setText(_translate("singlenumberinput", "查询"))
def run(self):
danhao = self.oddip.text()
a = asyncio.get_event_loop().run_until_complete(mainProgram2(danhao))
list1 = []
self.CourierCompany.setText(a['快递公司'])
for key in a:
list1.append(f'{key}:{a[key]}')
list2 = [str(k) for k in list1]
list3 = ' \n'.join(list2)
self.ReturnsResult.setText(f'{list3}')
def PopupWindow(self):
QMessageBox.information(w, '提示', '信号接收成功')
class MyWidget(QMainWindow):
# """
# 重写closeEvent方法
# """
def closeEvent(self, event):
result = QtWidgets.QMessageBox.question(self, "标题", "亲,你确定想关闭我?别后悔!!!'_'", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if result == QtWidgets.QMessageBox.Yes:
browser.close()
event.accept()
# 通知服务器的代码省略,这里不是重点...
else:
event.ignore()
async def mainProgram2(oddep):
odd = oddep
# await asyncio.sleep(1)
# await page.waitForSelector('input[id="J_SearchInput"]')
await page.type('input[id="J_SearchInput"]', odd)
await page.click('span[id="J_SearchBtn"]', button='left')
await asyncio.sleep(0.5)
# await page.waitForSelector('div[class="cp-detail"]>div[class="cp-name"]')
content = await page.content()
soup = BeautifulSoup(content, 'lxml')
res = soup.select('ul[id="J_PackageDetail"]>li>span[class="text"]')
list1 = []
list2 = []
dict1 = {}
for x in res:
list1.append(x.text)
res2 = soup.select('ul[id="J_PackageDetail"]>li>span[class="date"]')
for i in res2:
list2.append(i.text)
ResultsOf = soup.select('div[class="cp-name"]')
for i in ResultsOf:
# print(f'快递公司:{i.text}\n单号:{odd}记录')
# print('===================================')
# for j in range(len(list1)):
# print(f'{list2[j]}:{list1[j]}')
# print('****************************************************')
# print("信息来源:菜鸟裹裹-----阿里巴巴旗下菜鸟网络官方出品")
dict1['快递公司'] = str(i.text)
for o in range(len(list1)):
dict1[f'{list2[o]}'] = list1[o]
# print(dict1)
dict1['信息来源'] = '菜鸟裹裹-----阿里巴巴旗下菜鸟网络官方出品'
# await page.reload()
await page.evaluate('document.querySelector("#J_SearchInput").value=""')
# print(dict1)
# list3 = zip(list1, list2)
return dict1
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWidget()
UI = Ui_singlenumberinput()
slh = asyncio.get_event_loop()
UI.setupUi(w)
w.show()
app.exec_()