使用python codecs.Open打开联机txt文件
我正在尝试使用codecs.open打开联机txt文件。我现在的代码是:使用python codecs.Open打开联机txt文件,python,python-3.x,text,beautifulsoup,codec,Python,Python 3.x,Text,Beautifulsoup,Codec,我正在尝试使用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
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'
我试图用“\”替换“/”。它仍然不起作用。有办法解决吗?因为我有超过数千个链接要打开,所以我不太想将在线文本文件下载到本地驱动器中
如果有人能在这里帮助我,我将不胜感激
谢谢 你是不是在想这样的事情
`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`
编解码器。open
采用文件名,而不是url。谢谢,@larsks!我懂了。因为我想使用beautifulsoup来解析以txt格式编写的html。有什么方法可以做到这一点吗?使用http.client.HTTPConnection
您可以接收HTTPResponse
,它可以被包装(作为类似文件的对象)到codecs.EncodedFile
中。