使用python在特定时间请求网站



我用PyQt5编写了一个GUI,我想每天更新网站上的报价,并在GUI中更新此报价。

这是我向网站提出请求的主要位置:

if __name__ == "__main__":

app = QApplication(sys.argv)
screen_res = app.desktop().screenGeometry()
height = screen_res.height()
width = screen_res.width()
result = requests.get('https://somepage.com')
page = result.text
soup = BeautifulSoup(page, 'html.parser')
quote = soup.find('div', class_ = 'quote').text
quote = quote.replace(".", ". ")
window = MainWindow(width, height, quote)

sys.exit(app.exec_())

并且GUI:

class MainWindow(QMainWindow):
def __init__(self, width, height, quote, *args,**kwargs):



super(MainWindow, self).__init__(*args, **kwargs)
self.initUI(width, height, quote)
print('successfully built initUI...')
timer = QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
self.showDate()
print('successfully showed lcd...')

def initUI(self, width, height, quote):
self.lcd = QLCDNumber(self)
self.lcdDate = QLCDNumber(self)
self.lcd.setDigitCount(8)
self.lcdDate.setDigitCount(10)

self.label = QLabel(self)
self.label.setText(verse)
self.label.setFont(QFont('Times', 70, QFont.Bold))
self.label.setAlignment(Qt.AlignCenter)
self.label.setWordWrap(True)


self.setGeometry(0,0,width,height)
layout = QVBoxLayout()
layout.addWidget(self.lcd)
layout.addWidget(self.lcdDate)
layout.addWidget(self.label)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)

self.show()

def showlcd(self):
time = QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def showDate(self):
"""
date = QDate.currentDate()
textDate = date.toString(format = Qt.ISODate)
self.lcdDate.display(textDate)
"""
date = QDate.currentDate()
date.toString(Qt.ISODate)
final = date.toString('dd.MM.yyyy')
self.lcdDate.display(final)

我如何在午夜发出新请求并给我新报价?

类似这样的东西:

while True: 
getcurrentTime
if getcurrentTime == midnight:
make_new_request
update_quote_str
send_to_gui
break

我的问题是,当gui已经在运行时,我如何循环?然后以某种方式向gui 发送信号

逻辑不是使用while True,而是使用每T秒触发一次的计时器,并检查是否是获取";引用";。

为了简化任务,我已经将时间表库与QTimer一起使用;引用";非常耗时,因此它可以阻塞GUI,因此它必须在另一个线程中执行,最后您必须创建一个方法来更新GUI,而不创建新窗口。

import sys
import threading
import BeautifulSoup
import requests
import schedule
from PyQt5 import QtCore, QtGui, QtWidgets

class Crawler(QtCore.QObject):
quoteChanged = QtCore.pyqtSignal(str)
def start(self):
threading.Thread(target=self._execute, daemon=True).start()
def _execute(self):
result = requests.get("https://somepage.com")
page = result.text
soup = BeautifulSoup(page, "html.parser")
quote = soup.find("div", class_="quote").text
quote = quote.replace(".", ". ")
self.quoteChanged.emit(quote)

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
self.showDate()
def initUI(self):
self.lcd = QtWidgets.QLCDNumber(digitCount=8)
self.lcdDate = QtWidgets.QLCDNumber(digitCount=8)
self.label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter, wordWrap=True)
self.label.setFont(QtGui.QFont("Times", 70, QtGui.QFont.Bold))
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.lcd)
layout.addWidget(self.lcdDate)
layout.addWidget(self.label)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def set_quote(self, quote):
self.label.setText(quote)
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString("hh:mm:ss")
self.lcd.display(text)
def showDate(self):
date = QtCore.QDate.currentDate()
date.toString(QtCore.Qt.ISODate)
final = date.toString("dd.MM.yyyy")
self.lcdDate.display(final)

def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
crawler = Crawler()
crawler.quoteChanged.connect(w.set_quote)
schedule.every().day.at("00:00").do(crawler.start)
dt = 1000
schedule_timer = QtCore.QTimer(interval=dt, timeout=schedule.run_pending)
schedule_timer.start()
schedule.run_pending()
w.showMaximized()
app.exec_()

if __name__ == "__main__":
main()

相关内容

  • 没有找到相关文章

最新更新