本文实例讲述了python读取网页内容的方法。分享给大家供大家参考。具体如下:
import urllib2#encoding = utf-8class 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程序设计有所帮助。