简体   繁体   中英

Python - How to read the content of an URL twice?

I am using 'urllib.request.urlopen' to read the content of an HTML page. Afterwards, I want to print the content to my local file and then do a certain operation (eg constuct a parser on that page eg BeautifulSoup).

The problem After reading the content for the first time (and writing it into a file), I can't read the content for the second time in order to do something with it (eg construct a parser on it). It is just empty and I can't move the cursor( seek(0) ) back to the beginning.

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()

How can I fix it?

Thank you very much!

You can not read the response twice. But you can easily reuse the saved content:

content = response.read()
file.write(content)
print(content)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM