-2
私は 'urllib.request.urlopen'を使ってHTMLページの内容を読んでいます。その後、ローカルファイルにコンテンツを印刷してから、特定の操作(たとえば、そのページのパーサー、たとえばBeautifulSoup)を実行したいとします。Python - URLのコンテンツを2回読むにはどうすればいいですか?
問題 初めて内容を読み出す(およびファイルに書き込む)した後、私はそれで何かをするために、第2の時間のためのコンテンツを読み取ることができません(例えば、パーサの上の構築それ)。それはちょうど空であり、私はカーソルを最初に戻すことができません(シーク(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()
どうすれば修正できますか?
ありがとうございました!