我有一些 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/