Selenium安装与配置

Python 爬取B站(Bilibili.com)UP主的所有视频链接及详细信息
Python 爬取B站(Bilibili.com)UP主的所有公开视频链接及信息

安装selenium

安装selenium: conda install selenium

selenium + Edge

selenium + Edge 浏览器
Microsoft Edge WebDriver | Microsoft Edge Developer下载电脑edge浏览器对应版本的驱动
解压驱动,得到edgedriver_win64文件夹。
在C:\Program Files\Anaconda3\envs\my_env3.8\下新建文件夹selenium_EdgeDriver_win64
将edgedriver_win64文件夹放在该目录中。
测试代码:

from time import sleep
from selenium import webdriver

driverfile_path = "C:\Program Files\Anaconda3\envs\my_env3.8\selenium_EdgeDriver_win64\edgedriver_win64\msedgedriver.exe"
driver = webdriver.Edge(executable_path=driverfile_path)

driver.get('http://www.baidu.com/')

sleep(5)
driver.close()

或将驱动程序重命名为MicrosoftWebDriver.exe(因为 webdriver.py 里定义了浏览器驱动的名字是:MicrosoftWebDriver.exe。),然后放在python.exe同一目录下(环境变量配置好的目录下)。这样就不用executable_path参数。

selenium web自动化判断页面元素加载完毕
利用python+selenium带上cookies自动登录bilibili

linux无图形界面使用Selenium

ubuntu14虚拟机:

1、安装火狐浏览器:sudo apt install firefox

2、rz 调出文件传输工具,将windows下下载好的geckodriver-v0.29.1-linux64.tar.gz发给linux。(0.29.1 适配selenium3.141.0和firefox 66的版本)
tar -zxvf geckodriver-v0.29.1-linux64.tar.gz 解压文件
z: 表示使用gzip进行解压缩。
-x: 表示解压操作。
-v: 显示详细的解压缩过程。
-f: 指定要解压缩的文件名。

3、mv geckodriver /usr/bin 将解压得到的geckodriver文件移动到默认配置好环境变量的/usr/bin/的目录

4、geckodriver --version 即可查看geckodriver版本

5、pip3 install selenium 安装selenium 指定3.141.0版本

6、基本使用:

运行终端
python3
敲入以下代码
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# 配置Firefox选项 
firefox_options = Options()
firefox_options.headless = True # 以headless模式运行,具体代码如下:
 # 创建Firefox 
browser = webdriver.Firefox(options=firefox_options)
browser.get('https://www.baidu.com')
print(browser.page_source)
browser.quit()

实践中

firefox:webdriver的配置

from selenium import webdriver
...
options = webdriver.FirefoxOptions()
options.add_argument('--headless')    # 设置无界面  可选
options.add_argument("--disable-gpu")

profile = webdriver.FirefoxProfile()
profile.set_preference("dom.webdriver.enabled", False)  # 设置非driver驱动(跳过selenium检测)
#### 添加以下配置,解决内存占用增大问题
profile.set_preference("permissions.default.image", 2)  # 禁止下载图片,根据情况使用
# 禁用浏览器缓存
profile.set_preference("network.http.use-cache", False)
profile.set_preference("browser.cache.memory.enable", False)
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.sessionhistory.max_total_viewers", 3)
profile.set_preference("network.dns.disableIPv6", True)
profile.set_preference("Content.notify.interval", 750000)
profile.set_preference("content.notify.backoffcount", 3)
# 有的网站支持 有的不支持 2 35 profile.set_preference("network.http.pipelining", True)
profile.set_preference("network.http.proxy.pipelining", True)
profile.set_preference("network.http.pipelining.maxrequests", 32)

self.browser = webdriver.Firefox(options=options, firefox_profile=profile)

edge:webdriver的配置

from msedge.selenium_tools import Edge, EdgeOptions
...
driverfile_path = "C:\Program Files\Anaconda3\envs\my_env3.8\selenium_EdgeDriver_win64\edgedriver_win64\msedgedriver.exe"
options = EdgeOptions()
options.use_chromium = True  # 如果将 UseChromium 属性设置为 true,可以使用 EdgeOptions 类来访问在自动化其他Chromium浏览器时使用的相同的 特定于 Chromium 的属性和方法
# if headless:
#     options.add_argument('--headless')  # 设置无界面  可选
#     options.add_argument("--disable-gpu")
# 实现规避检测
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
# # 屏蔽webdriver特征    window.navigator.webdriver = false
# options.add_argument('--disable-blink-features=AutomationControlled')
self.browser = Edge(executable_path=driverfile_path, options=options)
self.browser.execute_cdp_cmd(  # window.navigator.webdriver = undefined
    'Page.addScriptToEvaluateOnNewDocument',
    {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'}
)
posted @   Bruce_JRZ  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
点击右上角即可分享
微信分享提示