Python implements methods for fetching documents from the web
- 2020-04-02 14:11:14
- OfStack
This article is an example of how a Python implementation can grab a document from a URL on the Web. Specific methods are analyzed as follows:
Example code is as follows:
import urllib
doc = urllib.urlopen("http://www.python.org").read()
print doc# Print out the web page directly
def reporthook(*a):
print a
# will http://www.renren.com Save to renre.html ,
# Call one word per block read reporthook function
urllib.urlretrieve("http://www.renren.com",'renren.html',reporthook)
# will http://www.renren.com Save to renre.html In the
urllib.urlretrieve("http://www.renren.com",'renren.html')
The results of the program are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
.......................... Web content
</body>
</html>
(0, 8192, -1)
(1, 8192, -1)
(2, 8192, -1)
Where urllib.urlopen returns a class file object.
I hope this article has helped you with your Python programming.