from selenium import webdriver
import time
# 设置 Chrome 驱动程序路径
driver_path = '/path/to/chromedriver'
# 目标搜索关键词
search_query = '需要搜索的内容'
# 设置 Chrome 浏览器选项
options = webdriver.ChromeOptions()
options.add_argument('--headless') # 无头模式运行浏览器,即不打开实际浏览器窗口
options.add_argument('--disable-gpu') #这个是禁用gpu
options.add_argument('--no-sandbox') # 这个是禁用沙箱
# 启动 Chrome 浏览器
driver = webdriver.Chrome(executable_path=driver_path, options=options)
try:
# 打开百度搜索页面
driver.get('https://www.baidu.com')
# 查找搜索框并输入关键词
search_box = driver.find_element_by_css_selector('input#kw') #这个代表的是搜索框的class
search_box.send_keys(search_query)
# 提交搜索表单
search_box
简单爬取百度页面以及爬出输入搜索内容案例
于 2024-07-12 17:32:24 首次发布