답변 2개
아래 샘플 참고하세요.
1. 파이썬 스크립트에서 진행률 업데이트 출력 파이썬 스크립트에서 `print()` 함수를 사용하여 진행률 업데이트를 출력하세요. 예를 들어, 다음과 같이 할 수 있습니다.
# 파이썬 스크립트
import time
# 진행률 업데이트 출력
for i in range(100):
print(i)
time.sleep(0.1)
2. PHP에서 진행률 업데이트 읽기 PHP에서 `exec()` 함수를 사용하여 파이썬 스크립트를 실행하고 진행률 업데이트를 읽으세요. 예를 들어, 다음과 같이 할 수 있습니다.
3. 프런트엔드에서 진행률 표시 PHP에서 출력된 진행률 업데이트를 프런트엔드(예: JavaScript)로 전달하여 진행률 표시줄이나 로딩 애니메이션을 업데이트하세요. 예를 들어, 다음과 같은 JavaScript 코드를 사용할 수 있습니다.
// PHP에서 출력된 진행률 업데이트 수신
var progress = ;
// 진행률 표시줄 업데이트
var progressBar = document.getElementById('progress-bar');
progressBar.value = progress;
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
아래의 코드가 도움이 도실지요...
1. PHP에서 exec() 사용
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// exec 명령어로 파이썬 스크립트 실행
$command = 'python3 your_script.py';
exec($command . ' > output.txt &'); // 결과를 output.txt 파일로 저장
echo json_encode(['status' => 'running']);
}
?>
2. AJAX로 요청 보내기
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Progress Bar</title>
<style>
#progress {
width: 0;
height: 20px;
background-color: green;
}
#progress-container {
width: 100%;
background-color: lightgray;
}
</style>
</head>
<body>
<button id="start">크롤링 시작</button>
<div id="progress-container">
<div id="progress"></div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
$('#start').click(function () {
$('#progress').css('width', '0');
// AJAX 요청
$.ajax({
url: 'your_php_script.php',
method: 'POST',
beforeSend: function () {
// 여기서 로딩 상태 표시 가능
$('#progress').css('width', '100%');
},
success: function (response) {
// 프로그레스 바 종료
$('#progress').css('width', '100%');
alert('크롤링이 완료되었습니다!');
}
});
});
});
</script>
</body>
</html>
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인