Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python在特定时间请求网站_Python_Python 3.x_Request_Pyqt5 - Fatal编程技术网

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

使用python在特定时间请求网站,python,python-3.x,request,pyqt5,Python,Python 3.x,Request,Pyqt5,我用PyQt5编写了一个GUI,我想每天更新一个网站的报价,并在GUI中更新这个报价 这是我向网站提出请求的主要原因: if __name__ == "__main__": app = QApplication(sys.argv) screen_res = app.desktop().screenGeometry() height = screen_res.height() width = screen_res.width() r

我用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_())
图形用户界面:

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秒触发一次,并检查是否是获取“报价”的时间

为了简化任务,我将schedule library与QTimer一起使用,此外,获取“quote”的任务非常耗时,因此它会阻塞GUI,因此必须在另一个线程中执行,最后,您必须创建一个更新GUI的方法,而无需创建新窗口

导入系统 导入线程 进口美联 导入请求 进口时间表 从PyQt5导入QtCore、QtGui、QtWidgets 类爬虫程序(QtCore.QObject): quoteChanged=QtCore.pyqtSignal(str) def启动(自): threading.Thread(target=self.\u execute,daemon=True).start() def_执行(自): 结果=请求。获取(“https://somepage.com") page=result.text soup=BeautifulSoup(第页,“html.parser”) quote=soup.find(“div”,class=“quote”).text quote=quote.替换(“.,”) self.quoteChanged.emit(quote) 类MainWindow(QtWidgets.QMainWindow): def uuu init uuu(self,parent=None): 超级(主窗口,自我)。\uuuuu初始化\uuuuuuu(父级) self.initUI() 定时器=QtCore.QTimer(自身) timer.timeout.connect(self.showlcd) 定时器启动(1000) self.showlcd() self.showDate() def initUI(self): self.lcd=QtWidgets.QLCDNumber(digitCount=8) self.lcdDate=QtWidgets.QLCDNumber(digitCount=8) self.label=qtwidts.QLabel(alignment=QtCore.Qt.AlignCenter,wordWrap=True) self.label.setFont(QtGui.QFont(“Times”,70,QtGui.QFont.Bold)) layout=qtwidts.QVBoxLayout() layout.addWidget(self.lcd) layout.addWidget(self.lcdDate) layout.addWidget(self.label) widget=qtwidts.QWidget() widget.setLayout(布局) self.setCentralWidget(小部件) def set_quote(自身,quote): self.label.setText(引号) def showlcd(自): time=QtCore.QTime.currentTime() text=time.toString(“hh:mm:ss”) self.lcd.display(文本) def显示日期(自我): date=QtCore.QDate.currentDate() date.toString(QtCore.Qt.ISODate) 最终=日期到字符串(“dd.MM.yyyy”) self.lcdDate.display(最终版) def main(): app=qtwidts.QApplication(sys.argv) w=主窗口() 爬虫程序=爬虫程序() 爬行器.quoteChanged.connect(w.set_quote) schedule.every().day.at(“00:00”).do(crawler.start) dt=1000 调度计时器=QtCore.QTimer(间隔=dt,超时=schedule.run\u挂起) 时间表\u timer.start() schedule.run_pending() w、 显示最大化() app.exec() 如果名称=“\uuuuu main\uuuuuuuu”: main()
这正是我需要的!非常感谢。