gpt4 book ai didi

python-3.x - 计算网页内的单词

转载 作者:行者123 更新时间:2023-12-04 14:41:33 24 4
gpt4 key购买 nike

我需要使用 python3 计算网页内的单词。我应该使用哪个模块?网址库?

这是我的代码:

def web():
f =("urllib.request.urlopen("https://americancivilwar.com/north/lincoln.html")
lu = f.read()
print(lu)

最佳答案

使用下面的自我解释代码,您可以获得一个很好的起点来计算网页中的单词:

import requests
from bs4 import BeautifulSoup
from collections import Counter
from string import punctuation

# We get the url
r = requests.get("https://en.wikiquote.org/wiki/Khalil_Gibran")
soup = BeautifulSoup(r.content)

# We get the words within paragrphs
text_p = (''.join(s.findAll(text=True))for s in soup.findAll('p'))
c_p = Counter((x.rstrip(punctuation).lower() for y in text_p for x in y.split()))

# We get the words within divs
text_div = (''.join(s.findAll(text=True))for s in soup.findAll('div'))
c_div = Counter((x.rstrip(punctuation).lower() for y in text_div for x in y.split()))

# We sum the two countesr and get a list with words count from most to less common
total = c_div + c_p
list_most_common_words = total.most_common()
例如,如果您想要前 10 个最常用的单词,则只需执行以下操作:
total.most_common(10)
在这种情况下输出:
In [100]: total.most_common(10)
Out[100]:
[('the', 2097),
('and', 1651),
('of', 998),
('in', 625),
('i', 592),
('a', 529),
('to', 529),
('that', 426),
('is', 369),
('my', 365)]

关于python-3.x - 计算网页内的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46271528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com