python - 为什么我的字计数器第一次运行时与第二次运行时产生不同的输出?

标签 python

我正在做一个基本的练习项目。我调用一个简单的维基百科页面,然后使用 Beautiful Soup 将所有内容写入文本文件。然后我计算一个单词在新编写的文本文件中出现的次数

由于某种原因,我第一次运行代码时得到的数字与第二次运行代码时得到的数字不同。

我相信第一次运行代码时,“anime.txt”与第二次运行代码时不同。

问题一定出在我使用 Beautiful Soup 收集所有文本数据的方式上。

请帮忙

from urllib.request import urlopen
from bs4 import BeautifulSoup

f = open("anime.txt", "w", encoding="utf-8")
f.write("")
f.close() 

my_url ="https://en.wikipedia.org/wiki/Anime"

uClient = urlopen(my_url)
page_html = uClient.read()
uClient.close()
page_soup = BeautifulSoup(page_html, "html.parser")
p=page_soup.findAll("p")


f = open("anime.txt", "a", encoding="utf-8")

for i in p:

    f.write(i.text)
    f.write("\n\n")

data= open("anime.txt", encoding="utf-8").read()
anime_count = data.count("anime")
Anime_count = data.count("Anime")

print(anime_count,"\n")
print(Anime_count, "\n")

count= anime_count+Anime_count

print("The total number of times the word Anime appears within <p> in the wikipedia page is : ", count)

第一个输出:

动漫数量 = 14

动漫数量 = 97

计数 = 111

第二个输出:

动漫数量 = 23

动漫数量 = 139

计数 = 162

编辑:

我根据前两条评论编辑了我的代码库,当然,它现在可以工作了:P。 对于以正确的方式/次数打开和关闭文件来说,这看起来更好吗?

from urllib.request import urlopen
from bs4 import BeautifulSoup

my_url ="https://en.wikipedia.org/wiki/Anime"

uClient = urlopen(my_url)
page_html = uClient.read()
uClient.close()
page_soup = BeautifulSoup(page_html, "html.parser")
p=page_soup.findAll("p")


f = open("anime.txt", "w", encoding="utf-8")

for i in p:

    f.write(i.text)
    f.write("\n\n")

f.close()

data= open("anime.txt", encoding="utf-8").read()
anime_count = data.count("anime")
Anime_count = data.count("Anime")

print(anime_count,"\n")
print(Anime_count, "\n")

count= anime_count+Anime_count

print("The total number of times the word Anime appears within <p> in the wikipedia page is : ", count)

最佳答案

不要对打开和关闭文件感到困惑。包括 with statements 中的所有写作/阅读部分.

from urllib.request import urlopen
from bs4 import BeautifulSoup

with open("anime.txt", "w", encoding="utf-8") as outfile:

    my_url ="https://en.wikipedia.org/wiki/Anime"

    uClient = urlopen(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = BeautifulSoup(page_html, "html.parser")
    p=page_soup.findAll("p")


    for i in p:
        outfile.write(i.text)
        outfile.write("\n\n")

with open("anime.txt", "r", encoding="utf-8") as infile:
    data = infile.read()
    anime_count = data.count("anime")
    Anime_count = data.count("Anime")

    print(anime_count,"\n")
    print(Anime_count, "\n")

    count= anime_count+Anime_count

    print("The total number of times the word Anime appears within <p> in the wikipedia page is : ", count)s : ", count)

关于python - 为什么我的字计数器第一次运行时与第二次运行时产生不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496973/

相关文章:

python - 如何解决 Red Hat 发行版中的区域设置问题?

python - Pandas 数据帧 : SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

python - 在 Python 中添加到本地命名空间?

python - pandas groupby 有两个键

python - 在查询中选择硬编码值的方法是什么?

python - 如何打印 FF(换页)字符?

python - WordNet Python 单词相似度

python - argparse 选项 arg 在子解析器中的位置 arg 之前使用 nargs ='?'

python - Kivy - 使用 .kv

python - 用python加入字节列表