gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:56:59 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com