Home >Backend Development >Python Tutorial >Python读取网页内容的方法

Python读取网页内容的方法

WBOY
WBOYOriginal
2016-06-10 15:08:241610browse

本文实例讲述了Python读取网页内容的方法。分享给大家供大家参考。具体如下:

import urllib2
#encoding = utf-8
class Crawler:
  def main(self):
    #req = urllib2.Request('http://www.baidu.com/')
    #req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0)')
    #urllib2.socket.setdefaulttimeout(10) # 超时10秒
    #page = urllib2.urlopen(req)
    page = urllib2.urlopen('http://www.google.com', timeout=10)
    data = page.read()
    print data
    print len(data) #计算字节长度
if __name__ == '__main__':
  me=Crawler()
  me.main()

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

Statement:
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