python脚本自动加载刷新网页

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 https://sites.google.com/a/chromium.org/chromedriver/home

Solution:

ChromeDriver - WebDriver for Chrome
or:
https://registry.npmmirror.com/binary.html?path=chromedriver/

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊Wa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值