你正在构建一个新的word_count
每节经文的字典,然后打印出来word_count
只为这节经文。相反,您只需要一个实例word_count
.
Update:代码还有其他问题,另外您应该使用正则表达式来删除所有非字母数字字符,另外您应该使用collections.Counter
,因为它使您的代码变得更短,而且,作为一个很好的副作用,让您可以检索最常见的单词:
import requests
import re
from bs4 import BeautifulSoup
from collections import Counter
def parse(url):
html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser")
count = Counter()
for bible_text in soup.findAll('font', {'class': 'tk4l'}):
text = re.sub("[^\w0-9 ]", "", bible_text.get_text().lower())
count.update(text.split(" "))
return count
word_count = parse('http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL=1&CN=1&CV=99')
print(word_count.most_common(10))
Output:
[('the', 83), ('and', 71), ('god', 30), ('was', 29), ('to', 22), ('it', 17), ('of', 16), ('there', 16), ('that', 15), ('in', 15)]