如何通过Python从Chrome获取网址? [英] How can I get a url from Chrome by Python?

查看:252
本文介绍了如何通过Python从Chrome获取网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Pywinauto来获取Chrome标签的url,如下所示:

I'm trying to use Pywinauto to get the Chrome tab's url like this:

(伪代码)

  1. 按F6定向网址行.
  2. Ctrl + C复制网址
  3. 从剪贴簿获取网址
  4. 通过BS4,请求,硒等处理URL.

我完成了步骤1并停留在步骤2.同样,我不知道如何处理步骤3.因此,认为我发现的方法不正确且效率不高. 有人可以建议我应该做什么或给我更好的方法吗?

I am done with step 1 and stuck at step 2. Again, I don't know how to deal with step 3. Thus, think the method I figure out is incorrect and not efficient. Can anybody suggest what I should do or give me a better way?

推荐答案

仅在一个答案中总结所有评论...

Just to summarize all the comments in one answer...

pywinauto 0.5.4没有TypeKeys和剪贴板抓取之类的技巧就无法从Chrome获取URL.即将来临的pywinauto 0.6.0将能够正确地做到这一点.

pywinauto 0.5.4 is not able to get the URL from Chrome without tricks like TypeKeys and clipboard grabbing. Coming pywinauto 0.6.0 will be able to do that right way.

在Chrome已经运行的情况下,只需使用connect而不是start.

Just use connect instead of start in case Chrome is already running.

我也建议使用TypeKeys("^c", set_foreground=False),因为URL在{F6}之后已经被聚焦,并且聚焦可能会切换到主窗口.

I would also recommend TypeKeys("^c", set_foreground=False) since the URL is already focused after {F6} and focus may switch to the main window.

Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome', found_index=0)也更好,因为标题应该是唯一的.如果有两个窗口,则found_index=01很有用.

Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome', found_index=0) is also better because the title should be unique. In case there are 2 windows found_index=0 or 1 is useful.

要获取剪贴板数据:

from pywinauto import clipboard
print(clipboard.GetData())

这篇关于如何通过Python从Chrome获取网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆