22.9.20

커피 내리는 그냥 사람·2022년 9월 20일
0

항해99

목록 보기
9/108

오늘 고민 많이 한 것

1. 작성한 게시글이 보이게 GET처리 하기

  • 항상 렌더를 받을 템플릿을 먼저 생각한다.
    주요 코드 및 에러
@app.route('/view/<num>')
def view(num):
    return render_template('view.html', num=num)

@app.route('/view_content/', methods=['GET'])
def view_content():
    num = request.args.get('num')
    int_num = int(num)
    content_view = [db.musics.find_one({'num': int_num}, {'_id': False})]
    return jsonify({'content_list': content_view})

주로 난 에러 : num에 관한 에러(숫자로 치환이 안 된다. 타입 에러 등)
고민 소요 시간 : 약 3~4시간
해결 방안 : 위 코드로 해결. 즉 렌더 상황과 실제 코드가 동작하는 것을 이분화하여 진행

js 코드 일부 : num을 jinja 처리 한 것 주목.

let num = '{{ num }}'
        $(document).ready(function () {
            listing();
        });
        function listing() {
            $("#mypost").empty()
            $.ajax({
                type: 'GET',
                url: '/view_content',
                data: {num},
                success: function (response) {
                    rows = response["content_list"]

2. 받은 크롤링 코드 통해 게시글 속에서 크롤링 후 자료 합쳐서 보내주기

팀원이 보내준 크롤링 코드를 그대로 이용하여 음반 정보를 크롤링 후 그 정보를 내가 만든 게시판 내용과 함께 DB에 저장하는 내용.

주요 고민 사항 : 다른 def를 어떻게 합칠 것인가?
해결 : 크롤링 코드를 한 번 더 사용한다.

@app.route("/content", methods=["POST"])
def content():
    album_receive = request.form["album_give"]

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
    params = {'axnm': album_receive}
    res = requests.get('https://www.genie.co.kr/detail/albumInfo', headers=headers, params=params)
    soup = BeautifulSoup(res.text, 'html.parser')
    s_artist = soup.select_one('div.info-zone > ul > li:nth-child(1) > span.value > a')
    s_img = soup.select_one('div.album-detail-infos > div.photo-zone > a > span.cover')
    s_tit = soup.select_one('#body-content > div.album-detail-infos > div.info-zone > h2')

    img = s_img.find('img')['src'][19:-19],
    title = s_tit.text.strip(),
    artist = s_artist.text


    content_list = list(db.musics.find({}, {'_id': False}))
    count = len(content_list) + 1
    title_receive = request.form["title_give"]
    content_receive = request.form["content_give"]
    star_receive = request.form["star_give"]

    doc = {
        "num" : count,
        "title": title_receive,
        "content": content_receive,
        "star": star_receive,
        'img': img[0],
        's_title': title[0],
        'artist': artist
    }

    db.musics.insert_one(doc)
    return jsonify({"result": "success", "msg": "업로드 완료!"})

이후 view에서는 모든 정보가 나올 수 있게 수정하고 write에서 필요한 정보값만 받아서 이용할 수 있게 data를 가공한다.

                data: {title_give: title, content_give: content, star_give: star, album_give: album},

실제 앨범에 대한 정보를 기입한 데이터

3. 기타 CSS 정리

  • 보기 싫은 선, 도형은 과감히 제거
  • 이후 그림 사이즈 img태그 이용하여 수정
profile
커피 내리고 향 맡는거 좋아해요. 이것 저것 공부합니다.

0개의 댓글