使用Python从网站读取文本文件

问题描述 投票:-1回答:1

您好我有问题我想从网上获取所有数据,但这太大了,无法将其保存到变量。我保存数据,如下所示:

r = urlopen("http://download.cathdb.info/cath/releases/all-releases/v4_2_0/cath-classification-data/cath-domain-list-v4_2_0.txt")
r = BeautifulSoup(r, "lxml")
r = r.p.get_text()
some operations

这一点很有效,直到我必须从这个网站获取数据:http://download.cathdb.info/cath/releases/all-releases/v4_2_0/cath-classification-data/cath-domain-description-file-v4_2_0.txt

当我在这个页面上运行与上面相同的代码时,我的程序正在停止

r = BeautifulSoup(r, "lxml")

这是永远的,没有任何事情发生。我不知道如何将这整个数据保存到文件中以进行搜索关键字并打印它们的一些操作。我无法将此保存到文件中我必须从网站上获取此信息。

我将非常感谢你的每一个帮助。

python-3.x web-scraping beautifulsoup
1个回答
1
投票

我认为下面的代码可以做你想要的。就像@alecxe的评论中提到的那样,你不需要使用BeautifulSoup。在线查找文本文件中的内容应该是一个问题,并在此In Python, given a URL to a text file, what is the simplest way to read the contents of the text file?中得到解答

import urllib.request import urlopen

r = urlopen("http://download.cathdb.info/cath/releases/all-releases/v4_2_0/cath-classification-data/cath-domain-list-v4_2_0.txt")

for line in r:
    do_somthing()
© www.soinside.com 2019 - 2024. All rights reserved.