Я пытаюсь массово загрузить текст, видимый «конечному пользователю» из 10-K SE C Отчеты Эдгара (не заботятся о таблицах) и сохранить его в текстовом файле. Я нашел код ниже на Youtube, однако я сталкиваюсь с двумя проблемами:
Я не уверен, что я захватываю весь текст, и когда я печатаю URL снизу, я получаю очень странный вывод (специальные символы, например, в самом конце распечатки)
Я не могу сохранить текст в txt
файлах, не уверен, что это из-за кодирования (я совершенно новичок в программировании).
import re
import requests
import unicodedata
from bs4 import BeautifulSoup
def restore_windows_1252_characters(restore_string):
def to_windows_1252(match):
try:
return bytes([ord(match.group(0))]).decode('windows-1252')
except UnicodeDecodeError:
# No character at the corresponding code point: remove it.
return ''
return re.sub(r'[\u0080-\u0099]', to_windows_1252, restore_string)
# define the url to specific html_text file
new_html_text = r"https://www.sec.gov/Archives/edgar/data/796343/0000796343-14-000004.txt"
# grab the response
response = requests.get(new_html_text)
page_soup = BeautifulSoup(response.content,'html5lib')
page_text = page_soup.html.body.get_text(' ',strip = True)
# normalize the text, remove characters. Additionally, restore missing window characters.
page_text_norm = restore_windows_1252_characters(unicodedata.normalize('NFKD', page_text))
# print: this works however gives me weird special characters in the print (e.g., at the very end)
print(page_text_norm)
# save to file: this only gives me an empty text file
with open('testfile.txt','w') as file:
file.write(page_text_norm)