Python 3.x 如何在Python中使用headless模式和selenium webdriver节省抓取数据的时间
您好,我有一个简单的python脚本,它可以自动打开并从网页中提取数据。 做这件事需要5秒钟。在我的情况下,我想一个更快的脚本运行瞬间或2秒最大 以下是脚本:Python 3.x 如何在Python中使用headless模式和selenium webdriver节省抓取数据的时间,python-3.x,selenium-webdriver,web-scraping,headless-browser,Python 3.x,Selenium Webdriver,Web Scraping,Headless Browser,您好,我有一个简单的python脚本,它可以自动打开并从网页中提取数据。 做这件事需要5秒钟。在我的情况下,我想一个更快的脚本运行瞬间或2秒最大 以下是脚本: #!/usr/bin/python3 # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui impo
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
options = Options()
options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")
url = 'https://www.coteur.com/match/cotes-barcelone-huesca-rid1163090.html'
driver = webdriver.Chrome(options=options)
driver.get(url)
odds = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, '//button[contains(@class, "btn btn-default btn-xs btncote")]')))]
columns = 3
rows = int(len(odds)/columns)
odds = [float(i) for i in odds]
odds = np.array(odds)
odds = odds.reshape(rows, columns)
print(odds, '\n')
driver.close()
driver.quit()
也许你可以帮助改进这个小脚本以节省宝贵的时间。
谢谢以下是执行的输出:
[[ 1.18 8.25 17. ]
[ 1.18 8.25 17. ]
[ 1.18 8.1 17. ]
[ 1.14 8. 17. ]
[ 1.16 8.75 18. ]
[ 1.2 7.25 10. ]
[ 1.14 7.75 16. ]
[ 1.17 8. 16. ]
[ 1.16 8.8 19. ]
[ 1.16 7. 12. ]
[ 1.13 8.5 18.5 ]]
real 0m4,978s
user 0m1,342s
sys 0m0,573s
运行它需要5秒钟。我的目标是减少执行时间您的执行时间可能取决于几个因素:
- 运行代码的机器
- 连接的带宽
- 您需要多少数据
2.31
秒
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
options = Options()
options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")
t0 = time.monotonic()
driver = webdriver.Chrome(options=options)
driver.get('https://www.coteur.com/match/cotes-barcelone-huesca-rid1163090.html')
elements = WebDriverWait(
driver,
2,
).until(
EC.visibility_of_all_elements_located(
(By.XPATH, '//button[contains(@class, "btn btn-default btn-xs btncote")]')
)
)
odds = np.array([float(my_elem.text) for my_elem in elements])
odds = odds.reshape(int(len(odds) / 3), 3)
print(odds)
t1 = time.monotonic()
print(f"{t1-t0:.2f}")
如果你问我的话,瞬间听起来像是一个非常紧迫的时间限制。即使我能节省2秒,也会很好,但为什么呢?您如何确定脚本执行的时间?这些数据变化很快。所以我想更快地提取它。我在运行python脚本之前使用命令时间。您可以在下面看到结果,谢谢您的回复