我对使用 microsoft Edge 作为网络驱动程序的 python selenium 网络抓取工具有问题。仅当存在其中之一时,msedgedriver 才能正常工作:
edge_options.add_argument("--user-data-dir=C:\\Users\\xxx\\Desktop\\Edge_user_data")
edge_options.add_argument('--headless')
当我尝试同时使用它们时,出现错误:
Traceback (most recent call last):
File "C:\Users\xxx\Desktop\test17.py", line 3414, in <module>
slower_process(urls, result_file_path)
File "C:\Users\xxx\Desktop\test17.py", line 3338, in slower_process
result = check_url(url)
^^^^^^^^^^^^^^
File "C:\Users\xxx\Desktop\test17.py", line 42, in check_url
result = 123_check(url, username)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Desktop\test17.py", line 534, in 123_check
driver = initialize_driver5()
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Desktop\test17.py", line 321, in initialize_driver5
driver = webdriver.Edge(options=edge_options, service=service)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__
super().__init__(
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 56, in __init__
super().__init__(
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in __init__
self.start_session(capabilities)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 290, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
self.error_handler.check_response(response)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: msedge failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from msedge location C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe is no longer running, so msedgedriver is assuming that msedge has crashed.)
Stacktrace:
GetHandleVerifier [0x00007FF74D583DB2+61490]
Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74D516002+740642]
(No symbol) [0x00007FF74D2EB8AE]
(No symbol) [0x00007FF74D31CBFE]
(No symbol) [0x00007FF74D317F13]
(No symbol) [0x00007FF74D35B7F8]
(No symbol) [0x00007FF74D3536E3]
(No symbol) [0x00007FF74D325EAA]
(No symbol) [0x00007FF74D32518B]
(No symbol) [0x00007FF74D326634]
Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF74D748D69+1207369]
(No symbol) [0x00007FF74D3A5304]
Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74D4690F1+32273]
Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74D4619E9+1801]
Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF74D747944+1202212]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74D51E998+19784]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74D51AE54+4612]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF74D51AF86+4918]
Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF74D50F451+713073]
BaseThreadInitThunk [0x00007FF87E917614+20]
RtlUserThreadStart [0x00007FF87FD226B1+33]
我在 StackOverflow 和 Git 上阅读了很多内容,但没有一个解决方案对我有用。这是我的代码:
def initialize_driver5(): # Microsoft Edge
# Define Edge options
edge_options = webdriver.EdgeOptions()
edgepath = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
edge_options.setBinary = edgepath
service = webdriver.EdgeService(service_args=['--log-level=SEVERE', '--disable-build-check'])
# Using the user profile
edge_options.add_argument("--user-data-dir=C:\\Users\\xxx\\Desktop\\Edge_user_data")
# Options
logger = logging.getLogger()
logger.setLevel(logging.CRITICAL)
#logging.basicConfig(level=logging.WARNING)
edge_options.add_argument('--remote-debugging-port=0')
edge_options.add_argument('--no-first-run')
edge_options.add_argument('--no-default-browser-check')
edge_options.add_argument('--headless')
edge_options.add_argument('--log-level=3')
edge_options.add_argument("--disable-logging")
edge_options.add_argument('--start-maximized')
edge_options.add_argument('--disable-infobars')
edge_options.add_experimental_option('excludeSwitches', ['disable-popup-blocking'])
driver = webdriver.Edge(options=edge_options, service=service)
return driver
我尝试过的:
我知道你已经解决了这个问题,但我想指出另一个解决方案,它不需要网络驱动程序。您可以使用
playwright
,这是一个非常新的 python 库,并且适用于许多浏览器,来安装 playwright
,运行以下命令:
pip install playwright
playwright install
然后,您可以使用此代码:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(channel="msedge")
page = browser.new_page()
page.goto('https://example.com')
browser.close()
这应该会给出所需的输出。要了解有关
playwright
的更多信息,请查看 文档。