使用Python浏览互联网

问题描述 投票:-1回答:1

我想用Python访问互联网。而且我还写了一个小程序。但是,我得到了许多对我来说没有意义的错误。

下面是程序。

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://google.com")

这里是错误。

"C:\Users\PycharmProjects\Python Tutorials\venv\Scripts\python.exe" "C:/Users/PycharmProjects/Python Tutorials/Facebook.py"
    Traceback (most recent call last):
      File "C:\Users\PycharmProjects\Python Tutorials\venv\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
        self.process = subprocess.Popen(cmd, env=self.env,
      File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
        self._execute_child(args, executable, preexec_fn, close_fds,
      File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
        hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
    FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "C:/Users/PycharmProjects/Python Tutorials/Facebook.py", line 10, in <module>
        driver = webdriver.Firefox()
      File "C:\Users\PycharmProjects\Python Tutorials\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
        self.service.start()
      File "C:\Users\PycharmProjects\Python Tutorials\venv\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
        raise WebDriverException(
    selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

Process finished with exit code 1

现在,我用了你的建议,它为我工作。我想我现在不仅可以打开google.com网站,还可以用我的ID和密码打开Facebook网站。

主要的问题是我无法安装webdrivermanager,因为,我发现有两个包,webdrivermanager和webdriver-manager。我一直在尝试安装webdrivermanager,在尝试安装的时候,它一直给我一个错误,但是当我安装webdriver-manager的时候,它允许我成功安装,现在它为我工作。

请你自己检查一下,这里。

我试图打开google.com网站,然后它自动关闭,然后它要求我提供登录ID和密码,然后它打开了另一个firefox网页,在那里它向我展示了一个Facebook的主页。

在这里,我还在前面的命令旁边加上#,并尝试运行,运行成功。我要感谢'geekforgeeks.com'网站,我从那里获得了大部分的代码。

python python-3.x selenium selenium-webdriver
1个回答
0
投票

Message: 'geckodriver' executable needs to be in PATH. this clearly mentioned that the geckodriver path is missing. 当你在Stackoverflow上搜索该信息时,你可以找到很多答案。

然而,我更倾向于使用Weddriver-manger库,它可以下载驱动程序,并采取必要的步骤,以最小的努力启动浏览器。

试试下面的片段。

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www.google.com")

driver.quit()

确保 Webdriver-manager 库添加到项目中。以下是pycharm的截图enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.