【问题标题】:Refresh a link from my python script刷新我的 python 脚本中的链接
【发布时间】:2015-09-28 00:05:49
【问题描述】:

我有一个链接,例如 www.google.com,我希望它每 2 秒后从我的 python 脚本中刷新一次。 python中是否有内置函数可以执行此操作,或者任何人都可以帮助我编写代码。

【问题讨论】:

  • 刷新是什么意思?刷新什么?
  • ctrl+r 是我所指的。我需要从脚本中自动完成,而无需我按下浏览器的刷新按钮
  • Javascript 可能是更好的选择。

标签: python refresh


【解决方案1】:

Windows 的 Python 2.7 答案:

import ctypes
from time import sleep

SendInput = ctypes.windll.user32.SendInput

# C struct redefinitions 
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

# Actuals Functions

def PressKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))


def RefreshPage():
    while True:
        PressKey(0x11) # Control
        PressKey(0x52) # R
        ReleaseKey(0x52)
        ReleaseKey(0x11) 
        time.sleep(2)

要查找其他关键代码: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

【讨论】:

  • 在哪里输入我的链接,在我输入的设定时间后刷新?
  • 我似乎误解了你的问题。我以为你提到过你想使用 Ctrl + R 来刷新你的页面。
  • 我是 python 新手坦率地说我没有正确理解你的代码。顺便说一句,我想要一个特定链接的自动刷新。假设我有一个 youtube 视频,我希望页面在 2 分钟左右后重新加载。可以通过 python 脚本完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多