[4.23 TIL] 크롤링한 결과를 API로 넘겨주기

박성규·2022년 4월 23일
1

[TIL]

목록 보기
2/2

앞선 포스트에서 각 음악 플랫폼별 노래 검색 결과값을 API로 프론트에 넘겨주자.

  1. 구상
    -POST 방식 이용, 결과 값을 돌려준다.
  1. 코드
[플라스크]
@app.route("/music-find", methods=["POST"])
def homework_post():
    name_receive = request.form['name_give']
    migrator = Migrator()
    migrator.crawl_list(name_receive)
    migrator.shutdown()
    return jsonify({'msg': migrator.get_data()})
[자바스크립트 ajax]
            let find_input = $('#find_input').val()
            if (find_input == '') {
                alert("내용을 입력해 주세요.")
            } else {
                $.ajax({
                    type: 'POST',
                    url: '/music-find',
                    data: {name_give: find_input},
                    success: function (response) {
                        console.log(response)
                    }
                })
            }
  1. 해석
    -플라스크에 music-find를 요청할 경우 migrator 클래스를 실행하여 크롤링 한다.
    -이후 크롤링된 값을 넘겨준다.
    -ajax에선 값을 받아서 console창에 띄어준다

  2. 문제점
    -해당되는 노래가 없을 경우도 생각해 줘야 하는데 그냥 결과를 찾기만 함

  3. 해결방안
    -검색 결과가 없을 경우에 대해 분리해서 작성되야 할듯. 다음 포스트에 해야지

    ※결과 화면※

0개의 댓글