实例代码如下:
import urllib doc = urllib.urlopen("http://www.python.org").read() print doc#直接打印出网页 def reporthook(*a): print a #将http://www.renren.com网页保存到renre.html中, #每读取一个块调用一字reporthook函数 urllib.urlretrieve("http://www.renren.com",'renren.html',reporthook) #将http://www.renren.com网页保存到renre.html中 urllib.urlretrieve("http://www.renren.com",'renren.html')
程序运行结果如下:
..........................网页内容 (0, 8192, -1) (1, 8192, -1) (2, 8192, -1)
其中urllib.urlopen返回一个类文件对象。
希望本文所述对大家的Python程序设计有所帮助。
查看更多关于python实现从web抓取文档的方法的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did87888