driver.quit()
使用 ActionChains 模拟用户操作
ActionChains 是 Selenium 提供的一个低级用户交互模拟工具,用于执行复杂的鼠标和键盘操作。它通过构建一系列动作(如鼠标移动、点击、拖拽、键盘按键等)的队列,模拟真实用户的操作流程。
下面模拟用户用鼠标点击百度顶部菜单“更多”中的“翻译”条目。
from selenium.webdriver.common.action_chains import ActionChains
# 获取相关元素
menu = driver.find_element(By.CSS_SELECTOR, "#s-top-left > div")
item = driver.find_element(By.CSS_SELECTOR, "#s-top-more > div:nth-child(1) > a")
# 定义动作链
actions = ActionChains(driver)
actions.move_to_element(menu).pause(1).click(item).perform() # pause(1) 悬停1秒后点击
模拟键盘输入
from selenium.webdriver.common.keys import Keys
# 模拟 Ctrl+C
actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
设置请求参数
在使用 Selenium 时,有些时候会因为自动化请求而遇到各种问题,或者想优化爬虫脚本,Options 是一个非常重要的工具,用于定制浏览器的各种行为。
设置无头模式
在服务器或无界面环境中运行爬虫,避免显式启动浏览器窗口,节省资源。
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument("--headless=new")
options.add_argument("--disable-gpu") # 部分系统需禁用 GPU 避免报错
service = Service(executable_path='D:/msedgedriver.exe')
driver = webdriver.Edge(service=service, options=options)
禁用浏览器弹窗和通知
网站存在弹窗广告、通知提示(如“允许通知”),干扰自动化操作。
options.add_argument("--disable-popup-blocking") # 禁用弹窗拦截
options.add_argument("--disable-notifications") # 禁用浏览器通知
设置代理服务器
options.add_argument("--proxy-server=http://127.0.0.1:8080")
忽略 HTTPS 证书错误
访问的网站使用自签名证书或存在证书错误,导致浏览器报错。
options.add_argument("--ignore-certificate-errors")
禁用扩展程序
浏览器默认安装的扩展(如广告拦截插件)干扰页面加载或元素定位。
options.add_argument("--disable-extensions") # 禁用所有扩展
options.add_argument("--disable-extensions-except=/path/to/extension") # 通过扩展 ID 禁用某个扩展
调整窗口大小和显示
需要模拟特定分辨率或最大化窗口。
options.add_argument("--start-maximized") # 启动时最大化窗口
options.add_argument("window-size=1920x1080") # 设置固定窗口大小
设置反爬虫策略
options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36") # 设置User-Agent
options.add_argument("--disable-blink-features=AutomationControlled") # 禁用浏览器指纹
实战案例:爬取徐州工业职业技术学院网站新闻
下面将使用 Selenium 库来爬取徐州工业职业技术学院网站上的新闻,并使用 jsonlines 库将爬取的数据输出为 .jsonl 文件。通过这个例子,可以学习到如何使用 Selenium 进行网页抓取、页面滚动控制、元素检查以及如何递归地遍历网站的所有链接。
核心功能函数
is_valid(): 检查URL是否属于目标站点的一部分。
is_element(): 判断指定元素是否存在。
smooth_scrol(): 控制页面平滑滚动,帮助加载所有动态内容。
fetch_page_content(url): 获取并返回指定URL的内容。
parse_articles_and_links(url): 解析页面中的文章信息和内部链接。
crawl(url): 主爬虫逻辑,递归调用自身处理新发现的链接。
通过这些函数,实现了从目标网站上自动提取新闻文章的功能,并将结果保存至本地文件。
数据提取与存储
对于每篇文章,脚本会提取标题、作者、日期和正文内容,并将其格式化后保存到 .jsonl 文件中。这种方法非常适合处理大量数据,因为 .jsonl 文件允许逐行添加记录,而不需要一次性加载整个文件。
with jsonlines.open(os.path.join(file_path, 'articles.jsonl'), mode='a') as f:
f.write(article)
递归的遍历网站
在这个脚本中,crawl() 函数是核心部分,它负责从一个起始 URL 开始,递归地探索并抓取整个网站的内容,从一个页面开始,提取其所有链接,并对每个新发现的有效链接重复这一过程,直到没有新的链接为止。
def crawl(url):
"""爬取页面"""
html_content = fetch_page_content(url)
if not html_content:
return
links = parse_articles_and_links(url)
for link in links:
crawl(link) # 递归调用自身处理新链接
在 crawl 函数内部,首先获取当前页面的内容(fetch_page_content),然后分析页面以提取文章信息和所有内部链接(parse_articles_and_links)。对于每一个新找到的有效链接,都会再次调用 crawl 函数进行处理。这样就形成了一个递归的过程,每次调用都进一步深入网站的一个新区域。当某个页面被访问过(存储在 visited_urls 列表中),或者没有更多的有效链接可探索时,对应的 crawl 调用就会结束,程序返回到上一级调用继续执行,直到最初的调用也完成为止。
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from urllib.parse import urljoin, urlparse
import os
import jsonlines
import re
import time
file_path = "output"
start_url = "https://www.xzcit.cn/"
visited_urls = [] # 已经访问过的URL
articles = []
service = Service(executable_path='D:/msedgedriver.exe')
driver = webdriver.Edge(service=service)
def is_valid(url):
"""检查URL是否属于目标站点"""
parsed = urlparse(url)
return bool(parsed.netloc) and 'www.xzcit.cn' in url
def is_element(by, value):
"""检查元素是否存在"""
driver.find_element(by, value)
except:
return False
return True
def smooth_scrol(scroll_step=350, wait_time=0.1):
按照指定步长和等待时间进行页面滚动。
scroll_step: 每次滚动的像素数
wait_time: 每次滚动间的等待时间
driver.execute_script("window.scrollTo({top: 0});")
# 获取当前文档高度
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
for i in range(0, last_height, scroll_step):
driver.execute_script(f"window.scrollTo(0, {i});")
time.sleep(wait_time) # 控制滚动速度
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
def fetch_page_content(url):
"""获取页面内容"""
if url in visited_urls:
return None
print(f"Crawling: {url}")
visited_urls.append(url)
driver.get(url)
except:
return None
smooth_scrol()
html_content = driver.page_source
return html_content if html_content else None
def parse_articles_and_links(url):
"""提取文章信息和内部链接"""
links = []
if is_element(By.CLASS_NAME, 'article'):
title = driver.find_element(By.CLASS_NAME, 'arti_title').text
body = driver.find_element(By.CLASS_NAME, 'entry')
author = driver.find_element(By.CLASS_NAME, 'arti_publisher').text.replace('作者:', '')
dateStr = driver.find_element(By.CLASS_NAME, 'arti_update').text.replace('日期:', '')
date = time.strftime('%Y年%#m月%#d日', time.strptime(dateStr, '%Y-%m-%d'))
content = ''
for p in body.find_elements(By.TAG_NAME, 'p'):
if p.text != '':
content += p.text + '\n'
content = "".join(re.split('\xa0| ', content))
if content != '':
article = {'date': date, 'author': author, 'title': title, 'content': content}
articles.append(article)
print(article)
with jsonlines.open(os.path.join(file_path, 'articles.jsonl'), mode='a') as f:
f.write(article)
except Exception as e:
print("发生异常:", repr(e))
# 提取页面中的所有链接
for a in driver.find_elements(By.TAG_NAME, 'a'):
link = a.get_attribute("href")
full_link = urljoin(url, link)
if is_valid(full_link) and full_link not in visited_urls and full_link not in links :
links.append(full_link)
print(full_link)
return links
def crawl(url):
"""爬取页面"""
html_content = fetch_page_content(url)
if not html_content:
return
links = parse_articles_and_links(url)
for link in links:
crawl(link) # 递归调用自身处理新链接
if __name__ == '__main__':
if not os.path.exists(file_path):
os.makedirs(file_path)
crawl(start_url)
driver.quit()