在日常工作中,我们常常会遇到一些重复性的操作,比如每天打开固定的几个网页,或者需要频繁地对网页进行截图。如果你是一位Python开发者,并且希望有一种更高效的方式来处理这些任务,那么今天介绍的Qutebrowser绝对会让你眼前一亮。
Qutebrowser 是一个基于 Python 和 PyQt 开发的键盘驱动浏览器,它不仅支持 Vim 风格的快捷键操作,还允许用户通过编写 Python 脚本来扩展其功能。接下来,让我们一起深入了解这款强大的浏览器吧!
根据你的操作系统,安装 Qutebrowser 非常简单:
# Linux 用户
sudo apt-get install qutebrowser
# Mac 用户
brew install qutebrowser
# Windows 用户
pip install qutebrowser
# 获取最新版本
pip install --user --upgrade qutebrowser
o
- 打开 URLO
- 在新标签页打开 URLgo
- 编辑当前 URLgO
- 在新标签页编辑并打开当前 URLJ
, K
- 切换标签d
- 关闭标签u
- 恢复关闭的标签T
- 显示所有标签列表/
- 搜索n
- 下一个搜索结果N
- 上一个搜索结果f
- 显示链接快捷键H
- 后退L
- 前进gh
- 回到首页j
, k
- 上下滚动h
, l
- 左右滚动gg
- 回到顶部G
- 去到底部from qutebrowser.commands import runners
def open_multiple_pages():
urls = [
'https://www.python.org',
'https://github.com',
'https://stackoverflow.com'
]
for url in urls:
runners.CommandRunner().run(f':open {url}')
if __name__ == '__main__':
open_multiple_pages()
from qutebrowser.api import commands
import time
def take_smart_screenshot(url, output_path, wait_time=3):
"""
智能网页截图函数
参数:
- url: 要截图的网页地址
- output_path: 保存路径
- wait_time: 等待页面加载的时间(秒)
"""
# 打开页面
commands.navigate(url)
# 等待页面加载
time.sleep(wait_time)
# 获取页面高度
js_get_height = """
Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight
)
"""
height = commands.js(js_get_height)
# 设置窗口大小
commands.set_size(width=1920, height=height)
# 截图
commands.screenshot(output_path, full=True)
# 使用示例
take_smart_screenshot('https://www.python.org', 'python_org_full.png')
c.content.blocking.enabled = True
c.content.blocking.method = 'both'
c.content.blocking.adblock.lists = [
'https://easylist.to/easylist/easylist.txt',
'https://easylist.to/easylist/easyprivacy.txt',
'https://secure.fanboy.co.nz/fanboy-cookiemonster.txt',
'https://easylist-downloads.adblockplus.org/antiadblockfilters.txt'
]
# 自定义过滤规则
c.content.blocking.whitelist = [
'*://*.trusted-site.com/*'
]
# 禁用JavaScript的网站
config.set('content.javascript.enabled', False, '*://*.annoying-ads.com/*')
from qutebrowser.api import commands, message
def inspect_element(selector):
"""
检查页面元素,用于爬虫调试
"""
js_code = f"""
(function() {{
let element = document.querySelector('{selector}');
if (element) {{
console.log('Element found:', element.outerHTML);
return {{
'text': element.textContent,
'html': element.outerHTML,
'attributes': Object.assign({{}}, ...Array.from(element.attributes)
.map(attr => ({{[attr.name]: attr.value}})))
}};
}}
return null;
}})()
"""
result = commands.js(js_code)
message.info(f'Element info: {result}')
# 使用示例
inspect_element('#main-content')
config.bind(',d', 'download-clear') # 清除下载历史
config.bind(',p', 'print') # 打印当前页面
config.bind(',s', 'view-source') # 查看源代码
def auto_fill_form(form_data):
"""
智能表单填充器
"""
js_code = """
function fillForm(data) {
for (let [selector, value] of Object.entries(data)) {
let element = document.querySelector(selector);
if (element) {
element.value = value;
// 触发change事件
let event = new Event('change', { bubbles: true });
element.dispatchEvent(event);
}
}
}
fillForm(%s);
""" % str(form_data)
commands.js(js_code)
# 使用示例
form_data = {
'#username': 'your_username',
'#password': 'your_password',
'input[name="email"]': '[email protected]'
}
auto_fill_form(form_data)
def save_session(name):
"""保存当前会话"""
commands.session_save(name)
def load_session(name):
"""加载指定会话"""
commands.session_load(name)
# 工作区切换示例
def switch_to_work():
load_session('work') # 加载工作相关的标签页
def switch_to_entertainment():
load_session('entertainment') # 加载娱乐相关的标签页
Qutebrowser 提供了强大的功能和高度的可定制性,非常适合那些希望通过编程方式提升工作效率的用户。无论是自动化测试还是日常浏览,Qutebrowser 都能成为你得力的助手。希望这篇博客能够帮助你更好地理解和利用 Qutebrowser 的强大功能!