亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python激活同名窗口

Python激活同名窗口

慕桂英546537 2023-07-27 15:56:09
選擇器.org fn是錯誤的,應該.org.fn選擇所有具有 classorg和 的元素fn。但是,有些項目沒有.address這樣,您的代碼會產生傾斜的結果。您可以使用此示例來獲取標題和地址(如果缺少地址,-則使用 ):import pprintimport requestsfrom itertools import chainfrom bs4 import BeautifulSoupres = requests.get('https://www.paginebianche.it/toscana/li/gommisti.html')res2 = requests.get('https://www.paginebianche.it/ricerca?qs=gommisti&dv=li&p=2')soup = BeautifulSoup(res.text, 'html.parser')soup2 = BeautifulSoup(res2.text, 'html.parser')hn = []for i in chain.from_iterable([soup.select('.item'), soup2.select('.item')]):    title = i.h2.getText(strip=True)    addr = i.select_one('[itemprop="address"]')    addr = addr.getText(strip=True, separator='\n') if addr else '-'    hn.append({'title': title, 'address': addr})    pprint.pprint(hn)印刷:[{'address': 'Via Don Giovanni Minzoni 44\n-\n57025\nPiombino (LI)',  'title': 'CENTROGOMMA'}, {'address': 'Via Quaglierini 14\n-\n57123\nLivorno (LI)',  'title': 'F.LLI CAPALDI'}, {'address': 'Via Ugione 9\n-\n57121\nLivorno (LI)',  'title': 'PNEUMATICI INTERGOMMA GOMMISTA'}, {'address': "Viale Carducci Giosue' 88/90\n-\n57124\nLivorno (LI)",  'title': 'ITALMOTORS'}, {'address': 'Piazza Chiesa 53\n-\n57124\nLivorno (LI)',  'title': 'Lo Coco Pneumatici'}, {'address': '-', 'title': 'PIERO GOMME'}, {'address': 'Via Pisana Livornese Nord 95\n-\n57014\nVicarello (LI)',  'title': 'GOMMISTA TRAVAGLINI PNEUMATICI'}, {'address': 'Via Cimarosa 165\n-\n57124\nLivorno (LI)',  'title': 'GOMMISTI CIONI AUTORICAMBI & SERVIZI'}, {'address': 'Loc. La Cerretella, 219\n-\n57022\nCastagneto Carducci (LI)',  'title': 'AURELIA GOMME'}, {'address': 'Strada Provinciale Vecchia Aurelia 243\n'             '-\n'             '57022\n'             'Castagneto Carducci (LI)',  'title': 'AURELIA GOMME DI GIANNELLI SIMONE'},...and so on.
查看完整描述

1 回答

?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

要循環選擇窗口,需要執行以下幾個步驟:

  • 使用win32gui.EnumWindows遍歷所有打開的窗口

  • 使用win32gui.GetWindowText從窗口獲取標題欄文本

  • 使用win32com.client.DispatchSendKeys激活切換過程

  • 使用win32gui.SetForegroundWindow選擇要激活的窗口

這是代碼:

import win32com.client as win32

import win32gui

import time


title = "Untitled - Notepad2"  # cycle all windows with this title


def windowEnumerationHandler(hwnd, top_windows):

    top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))

    

top_windows = []  # all open windows

win32gui.EnumWindows(windowEnumerationHandler, top_windows)


winlst = []  # windows to cycle through

for i in top_windows:  # all open windows

   if i[1] == title:

      winlst.append(i)

      

for x in range(5):  # cycle 5 times

   for w in winlst:  # each window with selected title

       shell = win32.Dispatch("WScript.Shell")  # set focus on desktop

       shell.SendKeys('%')  # Alt key

       win32gui.SetForegroundWindow(w[0]) # bring to front, activate

       time.sleep(2)  # 2 seconds


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號