我想下载这个文件到我的本地驱动器:https://www.sec.gov/Archives/edgar/data/1556179/0001104659-20-000861.txt
这是我的密码:
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')以下是错误消息:

那我该怎么下载这个文件呢?非常感谢!
发布于 2022-01-17 12:30:40
下载很好。问题是str(soup)没有很好的定义,并将html5lib抛到一个无休止的循环中。你可能是说
soup = soup.text它(粗略地)从BeatifulSoup对象中提取实际可读的文本。
发布于 2022-01-16 16:23:18
您的文件下载得很好;似乎BeautifulSoup的解析存在问题。尝试更改解析器并以如下方式进行:
path=r"https://www.sec.gov/Archives/edgar/data/1556179/0001104659-20-000861.txt"
r=requests.get(path, headers={"User-Agent": "b2g"})
soup=BeautifulSoup(r.text, "html.parser")
soup你会看到文件在那里。
https://stackoverflow.com/questions/70730948
复制相似问题