在这里完成初学者。以下代码旨在分析网站中的p标签(使用Python)并显示该网站的阅读水平。
#import both BS4 and the new URLLIB using the added .request
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
#credit to AbigailB (https://stackoverflow.com/users/1798848/abigailb)
def syllables(word):
count = 0
vowels = 'aeiouy'
word = word.lower().strip(".:;?!")
if word[0] in vowels:
count += 1
for index in range(1,len(word)):
if word[index] in vowels and word[index-1] not in vowels:
count += 1
if word.endswith('e'):
count -= 1
if word.endswith('le'):
count+=1
if count == 0:
count += 1
return count
#site prompt, to be replaced by active tab browser address
#site = input("Enter the website to find out its reading level:")
#my_url = "{}".format(site)
#default site for testing
my_url = "https://en.wikipedia.org/wiki/Jane_Austen"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
#empty variables to be pushed w/ extracted, looped text
senNum = []
wordNum = []
syllNum = []
page_soup = soup(page_html, "html.parser")
page_soup.findAll("p")
paragraphs = page_soup.findAll("p")
#loop through every paragraph, do magic
for para in paragraphs:
para = para.text.strip()
paraSen = int(len(para.split('.')) - 1)
paraWord = int(len(para.split()))
paraSyll = syllables(para)
intParaSen = int(paraSen)
intParaWord = int(paraWord)
intParaSyll = int(paraSyll)
#append stripped values into empty variables
senNum.append(intParaSen)
wordNum.append(intParaWord)
syllNum.append(intParaSyll)
#sums of all previously empty values
sumSenNum = sum(senNum)
sumWordNum = sum(wordNum)
sumSyllNum = sum(syllNum)
#averages for Flesch–Kincaid ease
avgWordsPerSen = sumWordNum/sumSenNum
avgSyllPerWord = sumSyllNum/sumWordNum
#final parts for Flesch–Kincaid ease
calcOne = avgWordsPerSen * 0.39
calcTwo = avgSyllPerWord * 11.8
finalCalc = calcOne + calcTwo - 15.59
print(finalCalc)
它在很大程度上取决于我在上面发现的标为def syllables(word)的代码块(在上面找到了贷项),该代码显示字符串中的音节数。它在某些站点上有效,但是在其他站点上运行代码时,出现以下错误:
Traceback (most recent call last):
File "C:\Users\Waves\Desktop\gradeLevel.py", line 48, in <module>
paraSyll = syllables(para)
File "C:\Users\Waves\Desktop\gradeLevel.py", line 10, in syllables
if word[0] in vowels:
IndexError: string index out of range
据我了解,它可能与[0]是数组中的第一个对象有关,而我相信原始作者的意思是暗示“如果没有元音分隔符...”,但我不确定。请随意与您对代码的任何无关的评论。先感谢您!
最佳答案
p
元素中有空文本
for para in paragraphs:
print(para)
# <p class="mw-empty-elt"> </p>
只是跳过那个
for para in paragraphs:
para = para.text.strip()
if not para:
continue