python - 将 url 中的 txt 文件读取到 BeautifulSOUP 中

标签 python beautifulsoup

我有一些 txt 文件的 url 地址,其中包含 html 代码。这是一个示例链接:

http://www.sec.gov/Archives/edgar/data/70858/000119312507058027/0001193125-07-058027.txt

我想用 BeautifulSoup 阅读这个 html 代码,代码如下:

from bs4 import BeautifulSoup
import urllib2 

url =    "http://www.sec.gov/Archives/edgar/data/70858/000119312507058027/0001193125-07-058027.txt"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
print (soup.prettify())

但是,我遇到了很多错误,例如:

File "C:/Users/.../aa.py", line 7, in <module> print (soup.prettify()) File "build\bdist.win32\egg\bs4\element.py", line 1097, in prettify return self.decode(True, formatter=formatter)

我怀疑会发生这种情况,因为 url 是 txt 文件而不是 html。我对吗?如果是这样,有人可以告诉我这里的解决方案是什么吗?

最佳答案

您可以尝试将文本文件的 HTML 部分(来自标签)输入到 Beautiful soup 中,我想它会崩溃,因为文本文件的开头不包含任何 HTML。

关于python - 将 url 中的 txt 文件读取到 BeautifulSOUP 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331224/

相关文章:

python - pymc3导入开关不起作用

python - 在 BeautifulSoup 中插入新构造的元素

python - beautifulsoup find_all() 类快捷方式不起作用

python - 每当我尝试运行 export_png 时, Bokeh 都会引发 RuntimeError

python - 谷歌合作实验室: Access Denied You don't have permission to access "website" on this server

python - Beautifulsoup 带有下拉菜单的网页抓取网站

python - 使用 BeautifulSoup 的不同 XML 元素名称列表

python - 查找 Pandas 中重复列的重复位置

python - Conda 安装搞砸了我的环境。我可以撤消它吗?

python - 在 Python 中使用枚举迭代到 len(list) - 1