gpt4 book ai didi

javascript - 仅从 html 页面中提取单词

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:40 25 4
gpt4 key购买 nike

我正在使用 python 2.7,我有一个包含 html 页面列表的文件夹,我只想从中提取单词。目前,我正在使用的过程是打开 html 文件,通过漂亮的汤库运行它,获取文本并将其写入新文件。但这里的问题是我仍然在输出中得到 javascript、css(body、colour、#000000 .etc)、符号(|、`、~、[] .etc)和随机数。

如何去除不需要的输出并只获取文本?

path = *folder path*
raw = open(path + "/raw.txt", "w")
files = os.listdir(path)
for name in files:
fname = os.path.join(path, name)
try:
with open(fname) as f:
b = f.read()
soup = BeautifulSoup(b)
txt = soup.body.getText().encode("UTF-8")
raw.write(txt)

最佳答案

可以去除脚本和样式标签

import requests
from bs4 import BeautifulSoup

session = requests.session()

soup = BeautifulSoup(session.get('http://stackoverflow.com/questions/27684020/extracting-only-words- from-html-pages').text)

#This part here will strip out the script and style tags.
for script in soup(["script", "style"]):
script.extract()

print soup.get_text()

关于javascript - 仅从 html 页面中提取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684020/

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