Python programming

(23.02.14.)Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ: Spring boot web -python jupyter notebook ์—ฐ๋™ํ•˜๊ธฐ, ํŒŒ์ด์ฌ json๋ชจ๋“ˆ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2023. 2. 15.

Spring Framework ์™€ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜์˜ ๊ธฐ๋Šฅ์„ ์ž‘์„ฑํ•œ๋‹ค


CrawlingTestController

๊ฐ„๋‹จํ•˜๊ฒŒ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ login ๋ฉ”์†Œ๋“œ

1.์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ๋‚ ์”จ๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ajax์š”์ฒญ์ด /craw/weather์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค

๋กœ๊ทธ์ธ์„ ๊ฑฐ์นœ ๊ฒฝ์šฐ์—๋งŒ ๋‚ ์”จ๋ณด๊ธฐ ๋ฒ„ํŠผ์ด ์ž‘๋™ํ•œ๋‹ค

ajax ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต

{title:๋‚ ์”จ์ •๋ณด,date:2023.02.15,'weather:๊ตฌ๋ฆ„์ด๋งŽ๊ณ , ์˜คํ›„์— ๋ง‘์•„์ง}


2. ํ”„๋ŸฐํŠธ์—”๋“œ์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝ์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์˜ ์‘๋‹ต์„ ์•„๋ž˜์ฒ˜๋Ÿผ ํ™”๋ฉด์— ํ‘œ์‹œํ•œ๋‹ค

<div>

<h3>๋‚ ์”จ์ •๋ณด</h3>

<div class='date'>2023.02.15</div>

<div class='weather'>๊ตฌ๋ฆ„์ด ๋งŽ๊ฒ ๊ณ , ์˜คํ›„์— ๋ง‘์•„์ง</div>

</div>


3. ์œ„์™€ ๊ฐ™์€ ์›น์‚ฌ์ดํŠธ์—์„œ ๋‚ ์”จ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ์ˆ˜์ง‘ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

ํ™”๋ฉด์— ์ œ๋ชฉ, ๋‚ ์งœ, ๋‚ ์”จ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•œ๋‹ค

 

 

์ž๋ฐ”-์ดํด๋ฆฝ์Šค ํŒŒํŠธ

 

CrawlingTestController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import jakarta.servlet.http.HttpSession;
 
@Controller
@RequestMapping("/crawl")
public class CrawlingTestController {
 
    @GetMapping("/login")
    public String loginForm()
    {
        return "thymeleaf/loginForm";
    }
    @PostMapping("/loginResult")
    @ResponseBody
    public Map<String,Boolean> loginResult(@RequestParam("id"String id, @RequestParam("pw"String pw, HttpSession session)
    {
       if(id.equals("smith")&&pw.equals("1234")) {
           session.setAttribute("login", id);
           Map<String,Boolean> map = new HashMap<>();
           map.put("login"true);
           return map;
       }
       return null;
    }
    
    @GetMapping("/weather")
    public String weather(HttpSession session)
    {
        Object obj = session.getAttribute("login");
        if(obj==null) {
            return null;
        }
        return "thymeleaf/weather";
    }
}
 
cs

 

loginForm.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๋กœ๊ทธ์ธ ํผ</title>
<style type="text/css">
    @import url(//fonts.googleapis.com/earlyaccess/jejumyeongjo.css);
    .jm-font{
        font-family: 'Jeju Myeongjo', cursive;/*์›น ํฐํŠธ ์ง€์ •*/
        color: black;
        font-size:3em;}
    #id, #pw{
        font-family: 'Jeju Myeongjo', serif;
        color: black;
        font-size:1.5em;}
    .wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;}
    main {width:fit-content; margin:1em auto; }
    main h3{ text-align: center;}
    form {border:1px solid black; padding:1em;}
    label {display:inline-block; width:3em; text-align: right; padding-right:1em; }
    div.btn { width:fit-content; margin: 0.5em auto; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js" 
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function getLogin()
{
    var obj = $('#loginForm').serialize();
    $.ajax({
        url : '/crawl/loginResult',
        method:'post',
        data : obj,
        cache:false,
        dataType:'json',
        success:function(res){
            alert(res.login ? '๋กœ๊ทธ์ธ ์„ฑ๊ณต':'๋กœ๊ทธ์ธ ์‹คํŒจ');
            if(res.login){
                location.href="/crawl/weather"
            }
        },
        error : function(xhr, status, err){
            alert('์—๋Ÿฌ:' + err);
        }
    });
    return false;
}
</script>
</head>
<body style="background-color: rgb(227,209,254);">
<div class="wrapper" >
<main class="main-font">
<h3 class="jm-font">๐Ÿง‘‍๐Ÿ’ป๋กœ๊ทธ์ธ</h3>
<form id="loginForm" onsubmit="return getLogin();">
    <div id ="id"><label for="userid">์•„์ด๋””</label>
        <input type="text" id="userid" name="id" style="height:25px">
    </div>
    <p>
    <div id="pw"><label for="userpwd">์•”ํ˜ธ</label>
        <input type="password" id="userpwd" name="pw" style="height:25px">
    </div>
    <div class="btn">
        <button type="reset">์ทจ์†Œ</button>
        <button type="submit">๋กœ๊ทธ์ธ</button>
    </div>
</form>
</main>
</div>
</body>
</html>
cs

 

weather.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์˜ค๋Š˜์˜ ๋‚ ์”จ</title>
</head>
<style type="text/css">
#weatherInfo{
    display:none;
}
</style>
<script type="text/javascript">
function showWeather(){
    var weatherInfo = document.getElementById("weatherInfo");
    weatherInfo.style.display="block";
}
</script>
<body>
<button type="button" onclick="javascript:showWeather();">๋‚ ์”จ๋ณด๊ธฐ</button>
<div id="weatherInfo">
    <h3>๋‚ ์”จ์ •๋ณด</h3>
    <div class="date">2023.02.15</div>
    <div class="weather">๊ตฌ๋ฆ„์ด ๋งŽ๊ฒ ๊ณ , ์˜คํ›„์— ๋ง‘์•„์ง</div>
</div>
</body>
</html>
cs

 

์‹คํ–‰๊ฒฐ๊ณผ : 

 

 

 

ํŒŒ์ด์ฌ-์ฅฌํ”ผํ„ฐ ๋…ธํŠธ๋ถ ํŒŒํŠธ

๊ฒฐ๊ณผ : 

 

 

 

json ๋ชจ๋“ˆ

์‹คํ–‰๊ฒฐ๊ณผ : 

 

 

 

์‹คํ–‰๊ฒฐ๊ณผ : 

๋Œ“๊ธ€