python实现从web抓取文档的方法

WBOY
Release: 2016-06-16 08:41:44
Original
975 people have browsed it

本文实例讲述了Python实现从Web的一个URL中抓取文档的方法,分享给大家供大家参考。具体方法分析如下:

实例代码如下:

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

Copy after login

程序运行结果如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
..........................网页内容
</body>
</html>


(0, 8192, -1)
(1, 8192, -1)
(2, 8192, -1)

Copy after login

其中urllib.urlopen返回一个类文件对象。

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template