enter link description here가 내 폴더에 .html
파일을 많이 가지고 내가 얻은 결과를 저장하려면 여기 제안 BeautifulSoup 스크립트를 개별 폴더 형태로 .txt
개의 파일에 저장합니다. 이 .txt
파일은 원본 파일과 이름이 같아야하지만 추출 된 내용 만 포함해야합니다. 필자가 쓴 스크립트는 파일을 성공적으로 처리하지만 추출 된 비트는 개별 파일에 쓰지 않습니다.
import os
import glob
from bs4 import BeautifulSoup
dir_path = "C:My_folder\\tmp\\"
for file_name in glob.glob(os.path.join(dir_path, "*.html")):
my_data = (file_name)
soup = BeautifulSoup(open(my_data, "r").read())
for i in soup.select('font[color="#FF0000"]'):
print(i.text)
file_path = os.path.join(dir_path, file_name)
text = open(file_path, mode='r').read()
results = i.text
results_dir = "C:\\My_folder\\tmp\\working"
results_file = file_name[:-4] + 'txt'
file_path = os.path.join(results_dir, results_file)
open(file_path, mode='w', encoding='UTF-8').write(results)
당신이 무슨 뜻인지 참조하십시오. 나는 그 일을하는 법을 몰랐다. 그냥,'AttributeError : 'module'이라는 메시지가 나타나면 스크립트에 오류가 있습니다. 'file'속성이 없습니다. 도움에 감사드립니다! – user3635159
@ user3635159 : 내 사과, 그건 내 잘못이야. 이제 해결되었습니다. –
건배, 지금은 효과가 있습니다. 나는 새로운 것을 배웠다. 감사. – user3635159