파이썬 web crawling 질문입니다.

조회수 1145회

안녕하세요, 프로그램을 만들고 테스트 하던 중에 버그를 발견했습니다.

제 프로그램에는 단어 빈도 수를 알려주는 기능과 이 포스트에서는 빠졌지만, 장수와 절수를 세주는 기능이 포함되어있습니다. 먼저, 단어 빈도 수를 알려주는 기능을 테스트 하던 중에 오류를 발견 했는데 고칠 수가 없어서 도움을 얻고자, 포스팅을 하게 됐습니다.

프로그램을 실행하면, 시작 점과 끝 점을 물어보는 input이 나오고 거기에 써 넣는데로 지정 되도록 했는데, 확인 해 보니 시작점이 제외 됨을 알 수 있었습니다. 가령 책49의 6장(마지막 장입니다) 부터 책50의 2장까지 지정을 하면, 49의 6장은 제외되고 50장의 1장부터 단어가 모아지는 것입니다.

이 문제를 어떻게 고칠 수 있을까요?

감사합니다.

from bs4 import BeautifulSoup
import operator
import requests
import re
import bs4


def word_frequency(max_book_w, max_chapter_w):
    word_list = []
    book = b
    chapter = c
    while book <= max_book_w:
        while chapter <= max_chapter_w:
            url = 'http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL={}&CN={}&CV=99'.format(book, chapter)
            source_code = requests.get(url).text
            soup = BeautifulSoup(source_code, "html.parser")
            for bible_text in soup.findAll('font', {'class': 'tk4l'}):
                content = bible_text.get_text()
                words = content.lower().replace('-', ' ').split()
                for each_word in words:
                    word_list.append(each_word)
            chapter += 1
        book += 1
        chapter = 1
    clean_up_list(word_list)


def clean_up_list(word_list):
    clean_word_list = []
    for word in word_list:
        symbols = "~:!@#$%^&*()_+`{}|\"?><`-=\][';/.,']"
        for i in range(0, len(symbols)):
            word = word.replace(symbols[i], "")
        if len(word) > 0:
            clean_word_list.append(word)
    dictionary(clean_word_list)


def dictionary(clean_word_list):
    word_count = {}
    for word in clean_word_list:
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1
    for key, value in sorted(word_count.items(), key=operator.itemgetter(1)):
        print(key, value)

user = int(input('''What do you need a help on?
          1 - word frequency, 2 - chapter count, 3 - verse count'''))
    if user == 1:
        b = int(input("type the starting book"))
        c = int(input("type the starting chapter of the book"))
        x = max_book_w = int(input("type the last book"))
        y = max_chapter_w = int(input("what chapter of the book would be the last chapter?"))
        word_frequency(x, y)
    elif user == 2:
        min_b = int(input("what is the starting book?"))
        max_b = int(input("what is the last book?"))
        chapter_counter(max_b)
    elif user == 3:
        books = int(input("which book do you want?"))
        chapters = int(input("which chapter of the book you want?"))
        if __name__ == '__main__':
            verse(books, chapters)

1st edit: 제가 49책의 5장을 포함해서 테스트 해 보았더니 5장만 제외 하는게 아니라, 49책의 5장과 6장을 둘다 제외시키고 결과를 보여주네요.,,

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 몇장몇챕터 부터 몇장몇챕터까지의 입력을받으려면 인풋이 4개여야하지 않나여 코드는 인풋이 2개만있는것 같네요 코드상에서도 처음입력한 정보를 저장하는 변수도없어보이구.. 초보입니다 파이썬은 안해봣어요 허허 ..

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)