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

Yur*_*ura -1 python urllib python-3.x

我使用'urllib.request.urlopen'来阅读HTML页面的内容.之后,我想将内容打印到我的本地文件,然后执行某个操作(例如,在该页面上构建解析器,例如BeautifulSoup).

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

import urllib.request   


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


file = open( "myTestFile.html", "w")
file.write( response.read()  )    # Tried responce.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()
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

非常感谢你!

wim*_*wim 7

你无法阅读两次回复.但您可以轻松地重复使用已保存的内容:

content = response.read()
file.write(content)
print(content)
Run Code Online (Sandbox Code Playgroud)