Сохранение текста годового отчета SE C 10-K в файлы (проблема с декодированием) - PullRequest
0 голосов
/ 13 апреля 2020

Я пытаюсь массово загрузить текст, видимый «конечному пользователю» из 10-K SE C Отчеты Эдгара (не заботятся о таблицах) и сохранить его в текстовом файле. Я нашел код ниже на Youtube, однако я сталкиваюсь с двумя проблемами:

  1. Я не уверен, что я захватываю весь текст, и когда я печатаю URL снизу, я получаю очень странный вывод (специальные символы, например, в самом конце распечатки)

  2. Я не могу сохранить текст в 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)

1 Ответ

0 голосов
/ 14 апреля 2020

Попробуйте это. Если вы возьмете данные, которые ожидаете, в качестве примера, людям будет легче понять ваши потребности.

from simplified_scrapy import SimplifiedDoc,req,utils
url = 'https://www.sec.gov/Archives/edgar/data/796343/0000796343-14-000004.txt'
html = req.get(url)
doc = SimplifiedDoc(html)
# text = doc.body.text
text = doc.body.unescape() # Converting HTML entities
utils.saveFile("testfile.txt",text)
...