使用python codecs.open打开在线txt文件

问题描述 投票:-1回答:1

我正在尝试使用codecs.open打开在线txt文件。我现在的代码是:

url = r'https://www.sec.gov/Archives/edgar/data/20/0000893220-96-000500.txt'
soup = BeautifulSoup(codecs.open(url, 'r',encoding='utf-8'), "lxml")

但是,Python不断提醒OSError:

OSError: [Errno 22] Invalid argument: 'https://www.sec.gov/Archives/edgar/data/20/0000893220-96-000500.txt'

我试图用“\”替换“/”。它仍然无法正常工作。有什么办法可以解决吗?由于我有超过数千个链接要打开,我不太想将在线文本文件下载到我的本地驱动器中。

如果有人能在这里提供帮助,我将非常感激。

谢谢!

python python-3.x text beautifulsoup codec
1个回答
1
投票

这是你想到的吗?

`from urllib.request import urlopen
url = urlopen('https://www.sec.gov/Archives/edgar/data/20/0000893220-96- 000500.txt')
 html = url.read().decode('utf-8')
 file = open('yourfile.txt', 'r')
 file.read(html)
 file.close`
© www.soinside.com 2019 - 2024. All rights reserved.