简体   繁体   English

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

[英]Open online txt file using python codecs.open

I am trying to open an online txt file using codecs.open. 我正在尝试使用codecs.open打开一个在线txt文件。 The code I have now is: 我现在拥有的代码是:

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

However, Python keeps reminding OSError: 但是,Python不断提醒OSError:

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

I tried to replace "/" with "\\". 我试图用“ \\”替换“ /”。 It still does not work. 它仍然不起作用。 Is there any way to solve it? 有什么办法解决吗? Since I have more than thousands of links to open, I do not quite want to download the online text files into my local drive. 由于我有成千上万的链接需要打开,因此我不想将在线文本文件下载到本地驱动器中。

I will appreciate it very much if someone can help here. 如果有人可以在这里提供帮助,我将非常感激。

Thanks! 谢谢!

Is it something like this you're thinking of? 您是否正在考虑这样的事情?

`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`

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

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