= array(); } $post['classname'] = 'post'; } function comment_format(&$post) { global $conf, $uid, $gid, $forumlist; if (empty($post)) return; $forum = $post['fid'] ? forum_read($post['fid']) : ''; $thread = well_thread_read_cache($post['tid']); if ($thread) { //$post['fid'] = $thread['fid']; $post['closed'] = $thread['closed']; $post['subject'] = $thread['subject']; $post['url'] = $thread['url']; } else { $post['closed'] = 0; $post['subject'] = lang('thread_not_exists'); $post['url'] = ''; } $post['create_date_fmt'] = humandate($post['create_date']); //$post['message'] = stripslashes(htmlspecialchars_decode($post['message'])); $user = user_read_cache($post['uid']); $post['username'] = array_value($user, 'username'); $post['user_avatar_url'] = array_value($user, 'avatar_url'); $post['user'] = $user ? user_safe_info($user) : user_guest(); isset($post['floor']) || $post['floor'] = 0; // 权限判断 $post['allowupdate'] = 2 == array_value($forum, 'comment', 0) && ($uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowupdate')); $post['allowdelete'] = group_access($gid, 'allowuserdelete') && $uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowdelete'); $post['user_url'] = url('user-' . $post['uid'] . ($post['uid'] ? '' : '-' . $post['pid'])); if ($post['files'] > 0) { list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($post['pid']); // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 if (2 == $conf['attach_on']) { foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $post['message'] = FALSE !== strpos($post['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $post['message']) : $post['message']; } } $post['filelist'] = $filelist; } else { $post['filelist'] = array(); } $post['classname'] = 'post'; } function comment_format_message(&$val) { global $conf; if (empty($val)) return; // 使用云储存 if (1 == $conf['attach_on'] && 1 == $val['attach_on']) { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } elseif (2 == $conf['attach_on'] && 2 == $val['attach_on']) { // 使用图床 list($attachlist, $imagelist, $filelist) = well_attach_find_by_tid($val['tid']); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $val['message'] = FALSE !== strpos($val['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $val['message']) : $val['message']; } } else { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } //$val['message'] = stripslashes(htmlspecialchars_decode($val['message'])); } // 把内容中使用了云储存的附件链接替换掉 function comment_message_replace_url($pid, $message) { global $conf; if (0 == $conf['attach_on']) { $message = FALSE !== strpos($message, '="../upload/') ? str_replace('="../upload/', '="upload/', $message) : $message; $message = FALSE !== strpos($message, '="/upload/') ? str_replace('="/upload/', '="upload/', $message) : $message; } elseif (1 == $conf['attach_on']) { // 使用云储存 $message = str_replace('="' . $conf['cloud_url'] . 'upload/', '="upload/', $message); } elseif (2 == $conf['attach_on']) { // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($pid); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换回相对链接 $message = $attach['image_url'] && FALSE !== strpos($message, $attach['image_url']) ? str_replace($attach['image_url'], $url, $message) : $message; } } return $message; } function comment_filter($val) { unset($val['userip']); return $val; } function comment_highlight_keyword($str, $k) { $r = str_ireplace($k, '' . $k . '', $str); return $r; } // //
function comment_message_format(&$s) { if (xn_strlen($s) < 100) return; $s = preg_replace('#.*?
#is', '', $s); $s = str_ireplace(array('
', '
', '
', '

', '', '', '', '' . ''), "\r\n", $s); $s = str_ireplace(array(' '), " ", $s); $s = strip_tags($s); $s = preg_replace('#[\r\n]+#', "\n", $s); $s = xn_substr(trim($s), 0, 100); $s = str_replace("\n", '
', $s); } // 对内容进行引用 function comment_quote($quotepid) { $quotepost = comment_read($quotepid); if (empty($quotepost)) return ''; $uid = $quotepost['uid']; $s = $quotepost['message']; $s = comment_brief($s, 100); $userhref = url('user-' . $uid); $user = user_read_cache($uid); $r = '
' . $user['username'] . ' ' . $s . '
'; return $r; } // 获取内容的简介 0: html, 1: txt; 2: markdown; 3: ubb function comment_brief($s, $len = 100) { $s = strip_tags($s); $s = htmlspecialchars($s); $more = xn_strlen($s) > $len ? ' ... ' : ''; $s = xn_substr($s, 0, $len) . $more; return $s; } ?>Python Selenium 各浏览器驱动下载与配置使用(详细流程)
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python Selenium 各浏览器驱动下载与配置使用(详细流程)

运维笔记admin45浏览0评论

大家好啊!我是NiJiMingCheng

这是我的博客:NiJiMingCheng

这节课我们来学习安装selenium和对应的各个浏览器驱动,个人比较喜欢使用谷歌浏览器驱动,所以接下来以谷歌浏览器来为大家做示例!!!
Selenium实战Python实现12306 抢票小助手(速通)



 

目录

1、安装

2、浏览器驱动下载

Chrome(google)浏览器驱动

3、下载浏览器驱动(webdriver)以谷歌浏览器为例

如有需要可以配置环境变量

4、驱动使用

5 、无头模式替换

Chrome 无头模式


1、安装

pip install selenium

2、浏览器驱动下载

Chrome(google)浏览器驱动
  1. 下载地址:http://chromedriver.storage.googleapis/index.html 或 https://sites.google/a/chromium/chromedriver/home
    下载地址:http://chromedriver.storage.googleapis/index.html 或 https://sites.google/a/chromium/chromedriver/home

  2. 下载地址 129 130 等版本:Chrome for Testing availability

Chrome浏览器驱动(chromedriver )最新版
Firefox浏览器驱动(geckodriver)
Edge浏览器驱动(MicrosoftWebDriver)
IE浏览器驱动(IEDriverServer)
Opera浏览器驱动(operadriver)
PhantomJS浏览器驱动(phantomjs),注意:这个无界面浏览器驱动已经不维护了,不推荐使用,selenium 后续版本也不用支持了。

3、下载浏览器驱动(webdriver)以谷歌浏览器为例

      根据自己的操作系统下载相对应的驱动,webdriver 需要和对应的浏览器版本以及 selenium 版本对应。
      例如当前电脑 Chrome 版本为 版本 131.0.6778.109(正式版本) (arm64),重点只需要看 131.0 后面的小版本号不需要管,打开 Chrome浏览器驱动(chromedriver ) 找到 131.0 开头的下载就行,选这个开头最新的那个。

        安装路径下载驱动后解压出来得到驱动可执行文件,不要运行,放到指定目录:

  • Windows:复制 webdriver 到 Python 安装路径的根目录下即可。
  • Mac:复制 webdriver 到 /usr/local/bin 或 /usr/bin 目录下,看有哪个目录。

        如果实在不清楚放哪里,但是想使用,那就随便找个文件夹一丢,通过自定义路径导入使用就行,比如项目根目录,直接通过路径访问驱动使用。

如下图我放入的位置

如有需要可以配置环境变量

4、驱动使用

Selenium实战-实现12306 抢票小助手(速通)

  • 验证浏览器驱动是否正常使用,确保 python 环境正常,selenium 包已经安装。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep

# 根据不同操作系统设置Chrome浏览器驱动路径,示例中仅给出了相对简单的示意,实际中可以根据需求更灵活判断

# Chrome浏览器配置选项,添加不自动关闭浏览器的实验性选项
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('detach', True)

# 创建Chrome浏览器驱动对象
driver = webdriver.Chrome(options=chrome_options)

# 如果要使用Firefox浏览器,取消下面这行注释即可
# driver = webdriver.Firefox()
# 如果要使用Edge浏览器,取消下面这行注释即可
# driver = webdriver.Edge()
# 如果要使用Internet Explorer浏览器,取消下面这行注释即可
# driver = webdriver.Ie()
# 如果要使用Opera浏览器,取消下面这行注释即可
# driver = webdriver.Opera()
# 如果要使用PhantomJS浏览器(注意它已停止维护,现在很少使用了),取消下面这行注释即可
# driver = webdriver.PhantomJS()

# 打开指定网址
driver.get('https://www.baidu')
# 休眠5秒
sleep(5)
# 关闭浏览器驱动对象
driver.quit()

5 、无头模式替换

  1. Chrome 无头模式
    基本概念:无头模式是指浏览器在运行时没有可见的用户界面。在这种模式下,浏览器可以在后台执行各种任务,如网页抓取、自动化测试等,而不会弹出浏览器窗口,节省系统资源并提高效率。
    代码实现:首先需要导入selenium库和相关模块,如下:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  • 然后设置ChromeOptions来启用无头模式:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
  • 接着,设置Chrome浏览器驱动路径(这里假设是在 Windows 系统下,路径为chromedriver.exe,你需要根据实际情况修改),并创建Service对象:
driver_path = 'chromedriver.exe'
service = Service(driver_path)
  • 最后,创建Chrome浏览器驱动对象并使用:
driver = webdriver.Chrome(service = service, options = chrome_options)
driver.get('https://www.baidu')
print(driver.page_source)
driver.quit()
  • 在上述代码中,--headless参数用于开启无头模式。通过driver.get方法访问网页后,可以使用driver.page_source获取网页的源代码,用于后续的分析,比如网页抓取等操作。
  1. Firefox 无头模式
    • 基本概念:同样是让 Firefox 浏览器在没有图形界面的情况下运行,适用于自动化任务和不需要可视化界面的场景。
    • 代码实现
      • 导入相关库:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
  • 设置FirefoxOptions来开启无头模式:
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument('-headless')
  • 设置Firefox浏览器驱动路径(假设是geckodriver.exe,根据实际情况修改),并创建Service对象:
driver_path = 'geckodriver.exe'
service = Service(driver_path)
  • 创建Firefox浏览器驱动对象并使用:
driver = webdriver.Firefox(service = service, options = firefox_options)
driver.get('https://www.baidu')
print(driver.page_source)
driver.quit()
  • 在 Firefox 中,-headless参数开启无头模式。和 Chrome 无头模式类似,通过driver.get方法访问网页后,可以获取网页源代码进行后续操作。

使用无头模式时的注意事项:

  • 性能优化:虽然无头模式可以节省资源,但在大量并发请求等情况下,仍需要合理配置资源,如设置合适的请求频率,避免给服务器造成过大压力或被目标网站视为恶意爬虫而封禁。
  • 兼容性:部分网站可能会对无头浏览器进行检测并限制访问,这种情况下可能需要采取一些反检测措施,如设置合适的用户代理(User - Agent)来模拟正常浏览器访问。

发布评论

评论列表(0)

  1. 暂无评论