Python - 如何读取 URL 的内容两次?

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

我正在使用“urllib.request.urlopen”来读取 HTML 页面的内容。之后,我想将内容打印到本地文件,然后执行某个操作(例如,在该页面上构造一个解析器,例如 Beautiful Soup)。

问题 第一次读取内容(并将其写入文件)后,我无法第二次读取内容以对其执行某些操作(例如,在其上构造解析器)。它只是空的,我无法将光标(seek(0))移回到开头。

import urllib.request   


response = urllib.request.urlopen("http://finance.yahoo.com")


file = open( "myTestFile.html", "w")
file.write( response.read()  )    # Tried response.readlines(), but that did not help me
#Tried: response.seek()           but that did not work
print( response.read() )          # Actually, I want something done here... e.g. construct a parser:
                                  # BeautifulSoup(response).
                                  # Anyway this is an empty result 


file.close()

我该如何修复它?

python python-3.x urllib
1个回答
7
投票

您不能将回复阅读两次。但您可以轻松地重复使用保存的内容:

content = response.read()
file.write(content)
print(content)
© www.soinside.com 2019 - 2024. All rights reserved.