我对编程很陌生,目前正在尝试学习Python。我的目标是使用网页抓取,或者更具体地说是BeautifulSoup来获取在dictionary.com上的一个单词的音节并计算有多少音节。通过这个,我想使用该函数来计算大文本文件中有多少个音节。但是,当我尝试在函数“syllables”中实现辅助函数“count_syllables”时,我似乎一直试图找出我的代码出错的地方。以下是我的代码和错误消息的一部分:
def count_syllables(keyword):
url = 'http://dictionary.com/browse/{}'.format(keyword)
web_object = requests.get(url)
text = web_object.text
text = text.encode('utf-8')
soup = BeautifulSoup(text, 'html.parser')
div = [div for div in soup.find_all('div', {'class': "waypoint-wrapper header-row header-first-row"})]
span = [div.find(name='span') for div in div]
span = span[0]['data-syllable']
strip_mid = span.replace('·', ' ')
syl = len(strip_mid.split())
return syl
grab_text = ['airplane.txt', 'sea.txt']
for file in grab_text:
with open(os.path.join("bigfile", file), "r") as infile:
all_text = infile.readlines()
entry = ''.join(all_text)
def syllables():
split_words = entry.split()
print('HERE ARE THE NUMBER OF SYLLABLES IN', file.upper())
for words in split_words:
words = count_syllables(split_words)
add_words = sum(words)
return add_words
print(syllables())
答案 0 :(得分:1)
看起来有时候
span = span[0]['data-syllable']
span将是一个空列表,因此您无法将其编入索引。
您需要实施某种检查。您可以执行try-catch,if语句或其他内容。