简体   繁体   English

从 EDGAR 下载一个 txt 文件

[英]Download a txt file from EDGAR

I want to download this file to my local drive: https://www.sec.gov/Archives/edgar/data/1556179/0001104659-20-000861.txt我想将此文件下载到我的本地驱动器: https://www.sec.gov/Archives/edgar/data/1556179/0001104659-20-000861.txt

Here are my codes:这是我的代码:

import requests
import urllib
from bs4 import BeautifulSoup
import re
  
path=r"https://www.sec.gov/Archives/edgar/data/1556179/0001104659-20-000861.txt" 
r=requests.get(path, headers={"User-Agent": "b2g"})
content=r.content.decode('utf8')
soup=BeautifulSoup(content, "html5lib")
soup=str(soup)
lines=soup.split("\\n")

dest_url=r"C://Users/YL/Downloads/a.txt"
fx=open(dest_url,'w')
for line in lines:
    fx.write(line + '\n')

Here is the error message:这是错误消息: 在此处输入图像描述

How should I download the file then?那我应该怎么下载文件呢? Thanks a lot!非常感谢!

Your file has downloaded alright;您的文件已下载正常; it seems there's a problem with BeautifulSoup's parsing. BeautifulSoup 的解析似乎有问题。 If you change the parser, instead of如果您更改解析器,而不是

content=r.content.decode('utf8')
soup=BeautifulSoup(content, "html5lib")
soup=str(soup)

use利用

stuff=r.text
soup=BeautifulSoup(stuff, "html.parser")
soup

and you'll see the file is there.你会看到文件在那里。

The download is fine.下载很好。 The problem is that str(soup) is not well-defined, and throws html5lib into an endless loop.问题是str(soup)没有明确定义,并将html5lib抛出一个无限循环。 You probably meant你可能是说

soup = soup.text

which (crudely) extracts the actual readable text from the BeatifulSoup object.它(粗略地)从 BeatifulSoup object 中提取实际可读文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM