提问人:yushi 提问时间:11/7/2023 更新时间:11/7/2023 访问量:23
我想通过异步通信运行截图功能
I want to run the screenshot function via asynchronous communication
问:
我想创建一个站点,在输入URL时输出URL目标和SSL证书的到期日期的屏幕截图。屏幕截图是使用 selenium 完成的。但是,使用硒的屏幕截图需要一些时间,因此我想在不等待屏幕截图结果的情况下显示站点结果,并在拍摄后立即输出屏幕截图。
selenium_function.py
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import os
import re
import sys
if len(sys.argv) != 2:
print("argument isnt two")
else:
url = sys.argv[1]
#headless
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--incognito')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=options)
driver.implicitly_wait(10) # seconds
driver.get(url)
sanitizedurl = re.sub(r'[\\/:*?"<>|]+','',url)
driver.get_screenshot_as_file(os.getcwd() + "\\ss\\" + sanitizedurl + "_small" + "_screenshot" + ".png")
width = driver.execute_script("return document.body.scrollWidth;")
height = driver.execute_script("return document.body.scrollHeight;")
driver.set_window_size(width,height)
driver.get_screenshot_as_file(os.getcwd() + "\\ss\\" + sanitizedurl + "_big" + "_screenshot" + ".png")
print(sanitizedurl)
测试.php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$url = $_POST['url'];
$url = urldecode($url);
$pythonScript = "selenium_function.py";
$command = "C:/Users/~~~~~~~/Python311/python.exe $pythonScript $url";
$output = exec($command);
$output_sanitizedurl = mb_convert_encoding($output, "UTF-8", "auto");
$date = "2024/01/01";
}
?>
<head>
<title>URL_SCREENSHOT</title>
</head>
<body>
<h1>URL_SCREENSHOT</h1>
<form method="post" action="test.php">
<label for="url">Input URL:</label>
<input type="text" name="url" id="url" required>
<input type="submit" value="take a screenshot">
</form>
<h2>SSL_DATE</h2>
<?php if(isset($date)){echo $date;}?>
<h3>Screenshot</h3>
<a href="./ss/<?php echo $output_sanitizedurl?>_big_screenshot.png" >
<img src="./ss/<?php echo $output_sanitizedurl?>_small_screenshot.png" width="100%">
</a>
</body>
</html>
我一直在尝试进行异步处理,并研究了 async/await 并尝试了它,但它没有按预期工作。我还想知道异步处理是否是我现在尝试做的事情的正确解决方案。 请帮忙。
答: 暂无答案
评论
$command = "C:/Users/~~~~~~~/Python311/python.exe $pythonScript $url"; $output = exec($command);