猿问

Python激活同名窗口

选择器.org fn是错误的,应该.org.fn选择所有具有 classorg和 的元素fn。


但是,有些项目没有.address这样,您的代码会产生倾斜的结果。您可以使用此示例来获取标题和地址(如果缺少地址,-则使用 ):


import pprint

import requests

from itertools import chain

from bs4 import BeautifulSoup



res = 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.


慕桂英546537
浏览 96回答 1
1回答

阿波罗的战车

要循环选择窗口,需要执行以下几个步骤:使用win32gui.EnumWindows遍历所有打开的窗口使用win32gui.GetWindowText从窗口获取标题栏文本使用win32com.client.Dispatch和SendKeys激活切换过程使用win32gui.SetForegroundWindow选择要激活的窗口这是代码:import win32com.client as win32import win32guiimport timetitle = "Untitled - Notepad2"  # cycle all windows with this titledef windowEnumerationHandler(hwnd, top_windows):    top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))    top_windows = []  # all open windowswin32gui.EnumWindows(windowEnumerationHandler, top_windows)winlst = []  # windows to cycle throughfor 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
随时随地看视频慕课网APP

相关分类

Python
我要回答