Python读取网页内容的方法

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

nohup后台启动Python脚本,log不刷新的解决方法

问题: =》nohup python3 xxxx.py &后台启动脚本 tail -100f nohup.out    -------->  &nbs...

Python 最强编辑器详细使用指南(PyCharm )

Python 最强编辑器详细使用指南(PyCharm )

作者:Jahongir Rahmonov 机器之心编译 参与:魔王 PyCharm 是一种 Python IDE,可以帮助程序员节约时间,提高生产效率。那么具体如何使用呢?本文从 PyC...

python处理csv数据动态显示曲线实例代码

本文研究的主要是python处理csv数据动态显示曲线,分享了实现代码,具体如下。 代码: # -*- coding: utf-8 -*- """ Spyder Editor...

python如何实现内容写在图片上

python如何实现内容写在图片上

本文实例为大家分享了python将内容写在图片上的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- # Created on 2018/3/20...

插入排序_Python与PHP的实现版(推荐)

插入排序Python实现 import random a=[random.randint(1,999) for x in range(0,36)] # 直接插入排序算法 def...