Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

웹 페이지를 크롤링하고 가장 빈번한 단어를 얻는 Python 프로그램

<시간/>

우리의 임무는 웹 페이지를 크롤링하고 단어의 빈도를 계산하는 것입니다. 그리고 궁극적으로 가장 자주 사용되는 단어를 검색합니다.

먼저 요청 및 아름다운 수프 모듈을 사용하고 이 모듈의 도움으로 웹 크롤러를 만들고 웹 페이지에서 데이터를 추출하고 목록에 저장합니다.

예시 코드

bs4에서 요청 가져오기 BeautifulSoupimport operatorfrom 컬렉션 가져오기 Counterdef my_start(url):my_wordlist =[] my_source_code =requests.get(url).text my_soup =BeautifulSoup(my_source_code, 'html.parser') for each_text in my_soup.findAll( 'div', {'class':'entry-content'}):content =each_text.text words =content.lower().split() for each_word in words:my_wordlist.append(each_word) clean_wordlist(my_wordlist)# 함수 원하지 않는 기호를 제거합니다.def clean_wordlist(wordlist):clean_list =[] for word in wordlist:symbols ='!@#$%^&*()_-+={[}]|\;:"<>?/., ' for i in range (0, len(symbols)):word =word.replace(symbols[i], '') if len(word)> 0:clean_list.append(word) create_dictionary(clean_list)def create_dictionary(clean_list ):word_count ={} for word in clean_list:if word in word_count:word_count[word] +=1 else:word_count[word] =1 c =Counter(word_count) # 가장 많이 발생하는 요소를 반환합니다. top =c.most_common(10) print(top)# 드라이버 codeif __name__ =='__main__':my_start("https://www.tutorialspoint.com/python3/python_overview.htm/") 

출력

<중앙> 웹 페이지를 크롤링하고 가장 빈번한 단어를 얻는 Python 프로그램