我是Python新手并且仍在努力学习,但我无法解决这个问题。我想在无限循环中运行一些函数(在类中)。因为它是一个QApplication,我已经知道我应该使用QTimer。但是,在探索如何做到这一点时,我找不到一个可行的替代方案。一个常见的解决方案是:
timer = QTimer()
timer.timeout.connect(function)
timer.start(60000)
但是当我将这些插入到我的代码中时,它没有任何区别。我试图将它插入函数,类等,但无法得到结果。循环的函数在这里:
__author__ = 'pc'
import requests
from bs4 import BeautifulSoup
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import sqlite3
import sys, getopt, time
from PyQt5.QtCore import QTimer
records = []
def scrape_page(url, html):
soup = BeautifulSoup(html, 'html.parser')
data = soup.find('div', class_='tablo_dual_board')
try:
datas = data.text.splitlines()
datas1 = list(filter(None, datas))
records.append(datas1)
except:
pass
def process_records():
# add record to database ...
print('process records:', len(records))
def generate_urls():
onexurl = "https://1xbahis19.com/en/live/Football/"
reply = requests.get(onexurl)
soup = BeautifulSoup(reply.content, "html.parser")
income = soup.find_all("ul", {"id":"games_content"})
links = soup.find_all("a", {"class": "c-events__name"})
urls = []
for matchlink in links:
urls.append("https://1xbahis19.com/en/"+(matchlink.get("href")))
return urls
class WebPage(QtWebEngineWidgets.QWebEnginePage):
def __init__(self):
super(WebPage, self).__init__()
self.loadFinished.connect(self.handleLoadFinished)
def start(self, urls):
self._urls = iter(urls)
self.fetchNext()
def fetchNext(self):
try:
url = next(self._urls)
except StopIteration:
return False
else:
self.load(QtCore.QUrl(url))
return True
def processCurrentPage(self, html):
scrape_page(self.url().toString(), html)
if not self.fetchNext():
process_records()
print(records)
QtWidgets.qApp.quit()
def handleLoadFinished(self):
self.toHtml(self.processCurrentPage)
app = QtWidgets.QApplication(sys.argv)
webpage = WebPage()
webpage.start(generate_urls())
timer = QTimer()
timer.timeout.connect(WebPage)
timer.start(60000)
app.exec_()
有人可以帮忙吗?
答案 0 :(得分:1)
我假设你想定期运行刮刀。下面的脚本将每60秒抓取一次所有网址。 seqinr
部分用于提供终止无限循环的方法 - 只需执行 Ctrl + C (即signal
),它将立即停止。
KeyboardInterrupt