吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3115|回复: 25
收起左侧

[Python 转载] 【Python】-selenium最基础的运行-另外浏览器添加各种设置(欢迎补充)

[复制链接]
surepj 发表于 2022-3-31 16:04
selenium是一个可以自动打开浏览器,
(需要安装浏览器驱动,我这里以Chrome为例。)
并进行各种操作的Python工具,
挺神奇的。
[Python] 纯文本查看 复制代码
import time
from selenium import webdriver

chrome_opt = webdriver.ChromeOptions()          # 引入设置
chrome_opt.add_argument('--start-maximized')    # 设置窗口最大化
browser = webdriver.Chrome(options=chrome_opt)  # 创建浏览器对象(传入设置参数)
browser.get('https://www.baidu.com/s?wd=时间')   # 发起访问网址
time.sleep(5)                                   # 延时5秒
browser.quit()                                  # 退出浏览器,结束进程;browser.close()不结束进程


下面是自己觉得一些有用浏览器设置:
设置按需求开启,欢迎大家交流一些好玩的操作,或者设置。
[Python] 纯文本查看 复制代码
# 最大化窗口(无头模式下无效,要window-size设置)
chrome_opt.add_argument('--start-maximized')

# 设置浏览器窗口大小
chrome_opt.add_argument('--window-size=1516,1030')

# 隐身模式(无痕模式)
chrome_opt.add_argument('--incognito')

# 关闭”正受到自动化测试软件的控制。“提示。与user-data-dir冲突
chrome_opt.add_experimental_option("--excludeSwitches", ['enable-automation'])

# 运行时不展示浏览器窗口(无头模式,后台运行)
chrome_opt.add_argument('--headless')

# 禁用GPU加速
chrome_opt.add_argument('--disable-gpu')

# 不加载图片, 提升速度
chrome_opt.add_argument('--blink-settings=imagesEnabled=false')

# 使window.navigator.webdriver=False,以防检测
chrome_opt.add_argument('--disable-blink-features=AutomationControlled')

# 用你常用的Chrome打开,否则是个全新的浏览器,没有任何收藏夹、插件等(前提是不能先打开任何Chrome浏览器(无头模式下好像无效))
chrome_opt.add_argument(r"--user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")

# 自动打开开发者模式(F12)
chrome_opt.add_argument("--auto-open-devtools-for-tabs")

# 设置请求头的User-Agent
chrome_opt.add_argument('--user-agent=52pojie')

免费评分

参与人数 3吾爱币 +2 热心值 +2 收起 理由
hubohang + 1 我很赞同!
夜步城 + 1 + 1 我很赞同!
options + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

jffwoo 发表于 2022-4-4 18:32
FileNotFoundError                         Traceback (most recent call last)
File D:\anaconda3\envs\learn-python3\lib\site-packages\selenium\webdriver\common\service.py:72, in Service.start(self)
     71     cmd.extend(self.command_line_args())
---> 72     self.process = subprocess.Popen(cmd, env=self.env,
     73                                     close_fds=platform.system() != 'Windows',
     74                                     stdout=self.log_file,
     75                                     stderr=self.log_file,
     76                                     stdin=PIPE)
     77 except TypeError:

File D:\anaconda3\envs\learn-python3\lib\subprocess.py:858, in Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    855             self.stderr = io.TextIOWrapper(self.stderr,
    856                     encoding=encoding, errors=errors)
--> 858     self._execute_child(args, executable, preexec_fn, close_fds,
    859                         pass_fds, cwd, env,
    860                         startupinfo, creationflags, shell,
    861                         p2cread, p2cwrite,
    862                         c2pread, c2pwrite,
    863                         errread, errwrite,
    864                         restore_signals, start_new_session)
    865 except:
    866     # Cleanup if the child failed starting.

File D:\anaconda3\envs\learn-python3\lib\subprocess.py:1311, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1310 try:
-> 1311     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1312                              # no special security
   1313                              None, None,
   1314                              int(not close_fds),
   1315                              creationflags,
   1316                              env,
   1317                              cwd,
   1318                              startupinfo)
   1319 finally:
   1320     # Child is launched. Close the parent's copy of those pipe
   1321     # handles that only the child should have open.  You need
   (...)
   1324     # pipe will not close when the child process exits and the
   1325     # ReadFile will hang.

FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
Input In [9], in <cell line: 6>()
      4 chrome_opt = webdriver.ChromeOptions()          # 引入设置
      5 chrome_opt.add_argument('--start-maximized')    # 设置窗口最大化
----> 6 browser = webdriver.Chrome(options=chrome_opt)  # 创建浏览器对象(传入设置参数)
      7 browser.get('https://www.baidu.com/s?wd=时间')   # 发起访问网址
      8 time.sleep(5)                                   # 延时5秒

File D:\anaconda3\envs\learn-python3\lib\site-packages\selenium\webdriver\chrome\webdriver.py:73, in WebDriver.__init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     66         desired_capabilities.update(options.to_capabilities())
     68 self.service = Service(
     69     executable_path,
     70     port=port,
     71     service_args=service_args,
     72     log_path=service_log_path)
---> 73 self.service.start()
     75 try:
     76     RemoteWebDriver.__init__(
     77         self,
     78         command_executor=ChromeRemoteConnection(
     79             remote_server_addr=self.service.service_url,
     80             keep_alive=keep_alive),
     81         desired_capabilities=desired_capabilities)

File D:\anaconda3\envs\learn-python3\lib\site-packages\selenium\webdriver\common\service.py:81, in Service.start(self)
     79 except OSError as err:
     80     if err.errno == errno.ENOENT:
---> 81         raise WebDriverException(
     82             "'%s' executable needs to be in PATH. %s" % (
     83                 os.path.basename(self.path), self.start_error_message)
     84         )
     85     elif err.errno == errno.EACCES:
     86         raise WebDriverException(
     87             "'%s' executable may have wrong permissions. %s" % (
     88                 os.path.basename(self.path), self.start_error_message)
     89         )

WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
几行代码都报错,坐等楼主解释
 楼主| surepj 发表于 2022-3-31 17:35
rijindoujin 发表于 2022-3-31 17:22
请教楼主,我是一个想要学习编程的新人。在这里下载的selenium破解版,怎么解压后都读取不了?能不能推荐一 ...

我也学着玩的,很多不懂。
selenium是Python的一个第三方库,免费的,不用破解啊。
cmd命令:pip install selenium可以直接安装的。
装完selenium,再去下载浏览器驱动chromedriver(也是免费的)再放到Python的安装目录即可
chromedriver下载地址(淘宝镜像)
https://registry.npmmirror.com/binary.html?path=chromedriver/
rijindoujin 发表于 2022-3-31 17:22
请教楼主,我是一个想要学习编程的新人。在这里下载的selenium破解版,怎么解压后都读取不了?能不能推荐一个适合32位电脑的selenium破解版。谢谢
夜步城 发表于 2022-3-31 18:45
浏览器设置代码好评
ofw 发表于 2022-3-31 20:25
学习一下
rijindoujin 发表于 2022-4-2 09:54
surepj 发表于 2022-3-31 17:35
我也学着玩的,很多不懂。
selenium是Python的一个第三方库,免费的,不用破解啊。
cmd命令:pip insta ...

谢谢坛主的热心回复,可我看不懂。三天了还没有下载运行一个软件,心情爆裂
 楼主| surepj 发表于 2022-4-2 13:15
rijindoujin 发表于 2022-4-2 09:54
谢谢坛主的热心回复,可我看不懂。三天了还没有下载运行一个软件,心情爆裂

没事,这个需要先学点Python基础,慢慢来。
 楼主| surepj 发表于 2022-4-4 21:25
jffwoo 发表于 2022-4-4 18:32
FileNotFoundError                         Traceback (most recent call last)
File D:\anaconda3\envs\ ...

可能是你没有把webdriver放入指定的路径吧
jffwoo 发表于 2022-4-5 15:49
surepj 发表于 2022-4-4 21:25
可能是你没有把webdriver放入指定的路径吧

我是通过anaconda安装的,应该不是这个问题
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 10:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表