使用python从chrome获取url

时间:2015-01-24 11:56:57

标签: python google-chrome url

我是python的新手,我正在尝试在Chrome中打印一个开放网站的网址。以下是我可以从这个页面收集到的内容并进行一些处理:

import win32gui, win32con
def getWindowText(hwnd):
   buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
   buf = win32gui.PyMakeBuffer(buf_size)
   win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buf)
   return str(buf)
hwnd = win32gui.FindWindow(None, "Chrome_WidgetWin_1" )
omniboxHwnd = win32gui.FindWindowEx(hwnd, 0, 'Chrome_OmniboxView', None)

print(getWindowText(hwnd))

我得到了这个结果:

<memory at 0x00CA37A0>

我真的不知道出了什么问题,他是否进入窗户以及我试图打印它的方式是错误的,或者他是否根本没有进入窗户。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

只有通过win32才能获取顶级应用程序的信息。如果要获取应用程序中每个组件的信息,可以使用UI Automation

Python具有包装器Python-UIAutomation-for-Windows,然后您可以通过以下代码获取浏览器的地址栏网址:

import uiautomation as auto


def get_browser_tab_url(browser: str):
    """
    Get browser tab url, browser must already open
    :param browser: Support 'Edge' 'Google Chrome' and other Chromium engine browsers
    :return: Current tab url
    """
    if browser.lower() == 'edge':
        addr_bar = auto.EditControl(AutomationId='addressEditBox')
    else:
        win = auto.PaneControl(Depth=1, ClassName='Chrome_WidgetWin_1', SubName=browser)
        temp = win.PaneControl(Depth=1, Name=browser).GetChildren()[1].GetChildren()[0]
        for bar in temp.GetChildren():
            last = bar.GetLastChildControl()
            if last and last.Name != '':
                break
        addr_bar = bar.GroupControl(Depth=1, Name='').EditControl()
    url = addr_bar.GetValuePattern().Value
    return url


print(get_browser_tab_url('Edge'))
print(get_browser_tab_url('Google Chrome'))
print(get_browser_tab_url('Cent Browser'))

答案 1 :(得分:0)

这应该有效:

import uiautomation as auto


control = auto.GetFocusedControl()
controlList = []
while control:
    controlList.insert(0, control)
    control = control.GetParentControl()
    
control = controlList[0 if len(controlList) == 1 else 1]
    
address_control = auto.FindControl(control, lambda c, d: 
                                            isinstance(c, auto.EditControl))

print('Current URL:')
print(address_control.GetValuePattern().Value)