Python
14
Вклад в тег
import time
import ctypes
import win32con
from ctypes import wintypes as w
KEYEVENTF_SCANCODE = 0x8
KEYEVENTF_UNICODE = 0x4
KEYEVENTF_KEYUP = 0x2
SPACE = 0x39
INPUT_KEYBOARD = 1
# not defined by wintypes
ULONG_PTR = ctypes.c_ulong if ctypes.sizeof(
ctypes.c_void_p) == 4 else ctypes.c_ulonglong
class KEYBDINPUT(ctypes.Structure):
_fields_ = [('wVk', w.WORD),
('wScan', w.WORD),
('dwFlags', w.DWORD),
('time', w.DWORD),
('dwExtraInfo', ULONG_PTR)]
class MOUSEINPUT(ctypes.Structure):
_fields_ = [('dx', w.LONG),
('dy', w.LONG),
('mouseData', w.DWORD),
('dwFlags', w.DWORD),
('time', w.DWORD),
('dwExtraInfo', ULONG_PTR)]
class HARDWAREINPUT(ctypes.Structure):
_fields_ = [('uMsg', w.DWORD),
('wParamL', w.WORD),
('wParamH', w.WORD)]
class DUMMYUNIONNAME(ctypes.Union):
_fields_ = [('mi', MOUSEINPUT),
('ki', KEYBDINPUT),
('hi', HARDWAREINPUT)]
class INPUT(ctypes.Structure):
_anonymous_ = ['u']
_fields_ = [('type', w.DWORD),
('u', DUMMYUNIONNAME)]
lib = ctypes.WinDLL('user32')
lib.SendInput.argtypes = w.UINT, ctypes.POINTER(INPUT), ctypes.c_int
lib.SendInput.restype = w.UINT
def send_scancode(code):
i = INPUT()
i.type = INPUT_KEYBOARD
i.ki = KEYBDINPUT(0, code, KEYEVENTF_SCANCODE, 0, 0)
lib.SendInput(1, ctypes.byref(i), ctypes.sizeof(INPUT))
i.ki.dwFlags |= KEYEVENTF_KEYUP
lib.SendInput(1, ctypes.byref(i), ctypes.sizeof(INPUT))
def send_unicode(s):
i = INPUT()
i.type = INPUT_KEYBOARD
for c in s:
i.ki = KEYBDINPUT(0, ord(c), KEYEVENTF_UNICODE, 0, 0)
lib.SendInput(1, ctypes.byref(i), ctypes.sizeof(INPUT))
i.ki.dwFlags |= KEYEVENTF_KEYUP
lib.SendInput(1, ctypes.byref(i), ctypes.sizeof(INPUT))
def find_notepad_window(notepad_title):
while True:
notepad_handle = ctypes.windll.user32.FindWindowW(None, notepad_title)
if notepad_handle != 0:
return notepad_handle
time.sleep(1)
def insert_text_into_notepad(notepad_handle, text):
# Развернуть окно, если оно свернуто
ctypes.windll.user32.ShowWindow(notepad_handle, win32con.SW_RESTORE)
# Сделать окно активным
ctypes.windll.user32.SetForegroundWindow(notepad_handle)
# Изменить заголовок окна
# ctypes.windll.user32.SendMessageW(notepad_handle, win32con.WM_SETTEXT, 0, text)
# send_scancode(SPACE)
# Текст в блокнот
send_unicode(text)
if __name__ == "__main__":
notepad_title = "Безымянный – Блокнот"
text_to_insert = "Привет, мир!"
notepad_handle = find_notepad_window(notepad_title)
insert_text_into_notepad(notepad_handle, text_to_insert)
import ctypes
def enum_windows_callback(hwnd, lParam):
window_title = ctypes.create_unicode_buffer(1024)
ctypes.windll.user32.GetWindowTextW(hwnd, window_title, 1024)
print(f"Window Handle: {hwnd}, Window Title: {window_title.value}")
return True
ctypes.windll.user32.EnumWindows(ctypes.WINFUNCTYPE(
ctypes.c_bool, ctypes.c_ulong, ctypes.c_ulong)(enum_windows_callback), 0)
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
URL = 'https://www.avito.ru/moskva/bytovaya_elektronika'
PAUSE_DURATION_SECONDS = 5
def main():
driver.get(URL)
sleep(PAUSE_DURATION_SECONDS)
if __name__ == '__main__':
try:
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
main()
except Exception as e:
print(e)
finally:
driver.quit()
dict_ = {"name": [["андрей", "2872"], ["михаил", "2872"]]}
del dict_['name'][-1]
print(dict_)
{'name': [['андрей', '2872']]}
def get_source_html(url):
chrome_options = Options()
driver = Service(executable_path="C:\\webdrivers\\chromedriver.exe")
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
try:
driver.get(url=url)
time.sleep(3)
while True:
find_more_element = driver.find_element(
By.CLASS_NAME, "catalog-button-showMore")
if driver.find_elements(By.CLASS_NAME, "hasmore-text"):
with open("lesson6/source-page.html", "w") as file:
file.write(driver.page_source)
break
else:
actions = ActionChains(driver)
actions.move_to_element(find_more_element).perform()
time.sleep(3)
except Exception as ex:
print(ex)
finally:
driver.close()
driver.quit()