使用 Python 和 PyAutoGUI 自动化 Bing 搜索

这篇博客文章将探讨如何利用 Python 和 PyAutoGUI 库来自动化 Microsoft Edge 上的简单网络搜索。我们将逐步讲解代码,解释其功能并演示其自动化重复任务的能力。

C:\pythoncode\new\autoguigoogle.py

全部代码:

python 复制代码
import pyautogui
import time

# Open Microsoft Edge
pyautogui.hotkey('win', 'r')
pyautogui.write('msedge')
pyautogui.press('enter')

# Go to bing.com
time.sleep(5)

# ... (Existing code for opening Edge, going to bing.com, etc.)

# Focus on the search bar
# time.sleep(5)
# pyautogui.press('tab')

# Switch to English input mode (adjust shortcuts as needed)

# pyautogui.hotkey('ctrl', 'shift')  # Common shortcut for language switching
# pyautogui.hotkey('alt', 'e')  # Specific shortcut for English (may vary)
# Enter search query
# pyautogui.write('copolit')

# ... (Rest of the existing code for pressing Enter, waiting, and capturing screenshot)

pyautogui.hotkey('ctrl', 'l')



pyautogui.write('bing.com')
pyautogui.press('enter')

# Focus on the search bar
time.sleep(5)
# pyautogui.press('tab')

# Enter search query
pyautogui.write('taylor swift')

# Press Enter to search
pyautogui.press('enter')

# Wait for results to load
time.sleep(5)

# Capture screenshot
full_screenshot = pyautogui.screenshot()
image_name = 'bing_search_results.png'
full_screenshot.save(image_name)

print("Screenshot saved:", image_name)

导入必要的库:

代码首先导入两个必要的库:

  • import pyautogui: 此库提供模拟鼠标和键盘操作的功能,允许我们自动化与计算机的用户交互。
  • import time: 此库使我们能够在操作之间引入延迟,模仿人类交互并确保脚本在网页加载完成后再继续执行。

打开 Microsoft Edge: 打开微软Edge:

  1. pyautogui.hotkey('win', 'r'): 此行模拟同时按下 Windows 键 (Win) 和字母"r"键,打开"运行"对话框。
  2. pyautogui.write('msedge'): 此行模拟在"运行"对话框中输入"msedge",指定启动 Microsoft Edge 的命令。
  3. pyautogui.press('enter'): 此行模拟按下 Enter 键,执行命令并启动 Microsoft Edge。

导航到 Bing:

  1. time.sleep(5): 此行引入 5 秒的延迟,允许 Microsoft Edge 启动并充分加载。虽然确切的加载时间可能有所不同,但此延迟可确保脚本在浏览器准备好之前不会继续执行。

替代方法(已注释):

已注释的部分 (# Focus on the search bar... # pyautogui.press('tab')) 展示了另一种聚焦搜索栏的方法,即使用 Tab 键。但是,在这种情况下,直接使用 pyautogui.hotkey('ctrl', 'l') 导航到地址栏可能更有效。

搜索"copolit"(已注释):

已注释的部分 (# Switch to English input mode... # pyautogui.write('copolit')) 展示了切换输入模式为英文(根据需要调整快捷键)然后输入搜索查询"copolit"的逻辑。

重复导航到 Bing:

  1. pyautogui.hotkey('ctrl', 'l'): 此行模拟按下 Ctrl+L,这通常会在 Web 浏览器中打开地址栏。
  2. pyautogui.write('bing.com'): 此行模拟在地址栏中输入"bing.com"。
  3. pyautogui.press('enter'): 此行模拟按下 Enter 键,导航到指定的 URL (bing.com)。 com)。

搜索"taylor swift" 搜索"泰勒·斯威夫特"

  1. time.sleep(5): 再次引入 5 秒的延迟以允许 Bing 网页完全加载。
  2. pyautogui.write('taylor swift'): 此行模拟在 Bing 搜索栏中输入"taylor swift"。
  3. pyautogui.press('enter'): 此行模拟按下 Enter 键,在 Bing 上启动对"taylor swift"的搜索。

捕获屏幕截图:

  1. time.sleep(5): 在捕获屏幕截图之前引入最终的 5 秒延迟,以允许搜索结果加载。
  2. full_screenshot = pyautogui.screenshot(): 此行使用 pyautogui.screenshot() 捕获整个屏幕的屏幕截图。
  3. image_name = 'bing_search_results.png': 此行定义了捕获的屏幕截图的文件名。
  4. full_screenshot.save(image_name): 此行将屏幕截图保存到指定的文件名 (bing_search_results.png)。

确认消息:

  • print("Screenshot saved:", image_name): 此行将确认消息打印到控制台,指示屏幕截图已成功保存并使用所选的文件名。

结果如下:

结论:

此代码演示了如何使用 Python 和 PyAutoGUI 自动化简单的 Web 浏览任务。虽然可能需要根据您的系统配置调整语言切换的特定键盘快捷键,但核心功能展示了如何自动化打开浏览器、导航到网页、执行搜索和捕获屏幕截图。这种方法可以适用于各种重复的基于 Web 的任务,节省您的时间和精力。

相关推荐
m0_734949796 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
m0_514520577 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey7 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
Polar__Star8 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
m0_743623929 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python
FreakStudio9 小时前
无硬件学LVGL—定时器篇:基于Web模拟器+MicroPython速通GUI开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
gCode Teacher 格码致知10 小时前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest
不要秃头的小孩10 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
科雷软件测试10 小时前
使用python+Midscene.js AI驱动打造企业级WEB自动化解决方案
前端·javascript·python
星越华夏10 小时前
python——三角函数用法
开发语言·python