Automatically loads and refreshes web pages -- Python script

224 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1. Method One

1) python script as follows:

# -*- coding: utf-8 -*-

import sys


from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class WebView(QWebEngineView):
    def __init__(self):
        super(WebView, self).__init__()
        #url = 'https://blog.51cto.com/u_15169037/2711577'   # Custom refresh page
        url = 'https://blog.csdn.net/weixin_45392081/article/details/107081775'
        self.load(QUrl(url))
        self.showMinimized()   #window minimizing
        self.show()
        self.thread = Worker()  # Create thread instance
        self.thread.sinOut.connect(self.reloadWeb)  # Signal binding slot function
        self.thread.start()  # Turn on thread


    def reloadWeb(self):
        self.reload()   #Refresh page


class Worker(QThread):
    sinOut = pyqtSignal()  # Create a new signal with parameters
    num = 0
    def __init__(self, parent=None):  # After the constructor creates an object, it is called automatically
        super(Worker, self).__init__(parent)


    def __del__(self):  # Analytic function-- Called when an object is deleted and recycled
        self.wait()

    def run(self):
        for i in range(1000):
            # issue a signal
            self.sinOut.emit()  # Send the parameter string to the signal and send it
            # Thread sleep 10 seconds
            self.sleep(10)
            Worker.num = Worker.num + 1
            print (str(Worker.num) + " refreshes")




if __name__ == '__main__':
    app = QApplication(sys.argv)
    web = WebView()
    print('### exec succeed !')
    sys.exit(app.exec_())


2) Environmental dependence

pip install PyQt5 -i  https://pypi.tuna.tsinghua.edu.cn/simple/
# If wrong : No module named 'PyQt5.QtWebEngineWidgets',Please continue to install the following dependent packages.
pip install PyQtWebEngine -i  https://mirrors.aliyun.com/pypi/simple/

2. Method Two

1) script as follows:

from time import sleep

from selenium import webdriver

driver= webdriver.Chrome()   # need to download the corresponding browser driver to the python installation directory
driver.get("https://blog.csdn.net/qq_27061049/article/details/90577597") # Custom refresh page

for i in range(10000): # Refresh times
    driver.refresh()  # Refresh page
    sleep(5) #  Refresh interval

2) Environmental dependence

error 1 as follows:

ModuleNotFoundError: No module named 'selenium'

pip install selenium -i  https://mirrors.aliyun.com/pypi/simple/

error 2 as follows:

Traceback (most recent call last): File "E:\miniconda3\envs\tools\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start stdin=PIPE) File "E:\miniconda3\envs\tools\lib\subprocess.py", line 707, in init restore_signals, start_new_session) File "E:\miniconda3\envs\tools\lib\subprocess.py", line 992, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the specified file.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "web_refresh2.py", line 5, in driver= webdriver.Chrome() # File "E:\miniconda3\envs\tools\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in init self.service.start() File "E:\miniconda3\envs\tools\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see sites.google.com/a/chromium.…

Solution:

ChromeDriver - WebDriver for Chrome

Put the downloaded browser driver into the path of Python interpreter: E:\miniconda3\envs\tools