Python 为什么selenium在我的系统上运行得这么慢?

Python 为什么selenium在我的系统上运行得这么慢?,python,selenium,web-scraping,Python,Selenium,Web Scraping,我有一个简单的网页抓取程序,可以搜索我从《自动化无聊的东西》一书中获取的查询。我做了两个版本,第一个版本是我做的,第二个版本几乎就是书中的内容。第一个是使用Selenium来处理网页打开部分,大约需要78秒才能完成。代码如下: #! python3 from bs4 import BeautifulSoup from msedge.selenium_tools import Edge, EdgeOptions import sys import pyperclip # Running a s

我有一个简单的网页抓取程序,可以搜索我从《自动化无聊的东西》一书中获取的查询。我做了两个版本,第一个版本是我做的,第二个版本几乎就是书中的内容。第一个是使用Selenium来处理网页打开部分,大约需要78秒才能完成。代码如下:

 #! python3
from bs4 import BeautifulSoup
from msedge.selenium_tools import Edge, EdgeOptions
import sys
import pyperclip

# Running a search in pypi.org
options = EdgeOptions()
options.use_chromium = True
driver = Edge(options=options)
driver.maximize_window()
if len(sys.argv) > 1:
    searchTerm = ' '.join(sys.argv[1:])         # The search terms are in the run command
else:
    searchTerm = pyperclip.paste()              # The search terms are in the clipboard

url = 'https://pypi.org/search/?q=' + searchTerm
driver.get(url)

# Getting all the links

soup = BeautifulSoup(driver.page_source, 'lxml')
results = soup.find_all('a', {'class': 'package-snippet'})

# Extracting the link and opening in new tabs

for i in range(len(results)):
    link = results[i]
    resultURL = 'https://pypi.org' + link.get('href')
    driver.execute_script("window.open()")
    # driver.switch_to_window(driver.window_handles[i+1])      This is to be deprecated and the below is one the new one
    driver.switch_to.window(driver.window_handles[-1])
    driver.get(resultURL)
第二个使用webbrowser并请求打开网页,只需14秒

#! python3
import sys
import pyperclip
import webbrowser
import requests
from bs4 import BeautifulSoup

# Getting the link that is to be opened
if len(sys.argv) > 1:
    searchTerm = ' '.join(sys.argv[1:])

else:
    searchTerm = pyperclip.paste()
url = 'https://pypi.org/search/?q=' + searchTerm

# Scraping the webpage

res = requests.get(url)
res.raise_for_status()

# Getting all the links

soup = BeautifulSoup(res.text, 'lxml')
results = soup.find_all('a', {'class': 'package-snippet'})

# Opening the links in new tabs
for i in range(len(results)):
    link = results[i]
    resultURL = 'https://pypi.org' + link.get('href')
    webbrowser.open(resultURL)
我用来学习python的笔记本电脑很旧,几乎没有土豆好。这是主要原因吗?还是只有在无法避免的情况下才应该使用硒(如填充表单等)?是因为我在使用Edge吗?我注意到的一件事是,如果我不打开新选项卡,速度会明显加快。

driver.get(resultur)
等待页面加载,
webbrowser.open(resultur)
可能不会。