편집 기록

편집 기록
  • 프로필 엽토군님의 편집
    날짜2019.01.02

    파이썬 웹크롤링 에러문제


    import requests
    
    from bs4 import BeautifulSoup
    
    import operator
    
    def start(url):
         word_list=[]
        source_code=requests.get(url).text
        soup=BeautifulSoup(source_code,'lxml')
        p_tag=soup.findAll('p',{'class':'tt-post-title'})
    
        for title_text in p_tag:
            content = title_text.text
            words=content.lower().split()
            for each_word in words:
                word_list.append(each_word)
        clean_up_list(word_list)
    
    def clean_up_list(word_list):
        clean_up_list=[]
        for word in word_list:
            symbols="`~!@#$%^&*()-=_+[]\{}|;',./:\"<>?"
            for i in  range(0,len(symbols)):
                word = word.replace(symbols[i],"")
            if len(word)>0:
            #    print(word)
                clean_word_list.append(word)
    
        create_dictionary(clean_word_list)
    
    def create_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)
    
    start('https://creativeworks.tistory.com')
    

    티스토리 블로그 따라서 파이썬 공부중인 학생입니다. 코딩중에 원인을 모를 에러가 발생해서 질문올립니다. 코드는 위와 같습니다 현재 MAC 에서 코딩을하고 ATOM IDE 를 이용중입니다. 파이썬 버전은 3.7.1 입니다. 위 코드를 컴파일 했을시

    Traceback (most recent call last):
      File "/Users/stronghu/Documents/test36.py", line 44, in <module>
        start('https://creativeworks.tistory.com')
      File "/Users/stronghu/Documents/test36.py", line 19, in start
        clean_up_list(word_list)
      File "/Users/stronghu/Documents/test36.py", line 31, in clean_up_list
        create_dictionary(clean_word_list)
    NameError: name 'clean_word_list' is not defined
    

    이런 에러를 발생 합니다. create_dictionary(clean_word_list)가 원래는 for 문 밖에 서 실행이 되어야 해서 위 코드와 같이 코딩을 했는데 create_dictionary(clean_word_list) 문이 for문 안으로 들어가면 문제 없이 컴파일이됩니다 왜그런지 이유를 아무리 생각해도 이해가 되질 않습니다.

  • 프로필 시티젠님의 편집
    날짜2018.12.27

    파이썬 웹크롤링 에러문제


    import requests
    
    from bs4 import BeautifulSoup
    
    import operator
    
    def start(url):
         word_list=[]
        source_code=requests.get(url).text
        soup=BeautifulSoup(source_code,'lxml')
        p_tag=soup.findAll('p',{'class':'tt-post-title'})
    
        for title_text in p_tag:
            content = title_text.text
            words=content.lower().split()
            for each_word in words:
                word_list.append(each_word)
        clean_up_list(word_list)
    
    def clean_up_list(word_list):
        clean_up_list=[]
        for word in word_list:
            symbols="`~!@#$%^&*()-=_+[]\{}|;',./:\"<>?"
            for i in  range(0,len(symbols)):
                word = word.replace(symbols[i],"")
            if len(word)>0:
            #    print(word)
                clean_word_list.append(word)
    
        create_dictionary(clean_word_list)
    
    def create_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)
    
    start('https://creativeworks.tistory.com')
    

    티스토리 블로그 따라서 파이썬 공부중인 학생입니다. 코딩중에 원인을 모를 에러가 발생해서 질문올립니다. 코드는 위와 같습니다 현재 MAC 에서 코딩을하고 ATOM IDE 를 이용중입니다. 파이썬 버전은 3.7.1 입니다. 위 코드를 컴파일 했을시

    Traceback (most recent call last):
      File "/Users/stronghu/Documents/test36.py", line 44, in <module>
        start('https://creativeworks.tistory.com')
      File "/Users/stronghu/Documents/test36.py", line 19, in start
        clean_up_list(word_list)
      File "/Users/stronghu/Documents/test36.py", line 31, in clean_up_list
        create_dictionary(clean_word_list)
    NameError: name 'clean_word_list' is not defined
    

    이런 에러를 발생 합니다. create_dictionary(clean_word_list)가 원래는 for 문 밖에 서 실행이 되어야 해서 위 코드와 같이 코딩을 했는데 create_dictionary(clean_word_list) 문이 for문 안으로 들어가면 문제 없이 컴파일이됩니다 왜그런지 이유를 아무리 생각해도 이해가 되질 않습니다.