上QQ阅读APP看书,第一时间看更新
With Python 2
- First, import the required module, urllib:
>>> import urllib
- With the urlopen method, you can download the web page:
>>> webpage = urllib.urlopen("https://www.packtpub.com/")
- We can read the file like a returned object with the read method:
>>> source = webpage.read()
- Close the object when it's done:
>>> webpage.close()
- Now we can print the HTML, which is in a string format:
>>> print source
- It is very easy to update the program to write the contents of the source string to a local file on your computer:
>>> f = open('packtpub-home.html', 'w') >>> f.write(source) >>> f.close