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程序设计有所帮助。

相关文章

Linux(Redhat)安装python3.6虚拟环境(推荐)

python是3.6 centos 6 64位 1.安装python 2.安装pip wget https://bootstrap.pypa.io/get-pip.py --no-c...

12步教你理解Python装饰器

通过下面的步骤让你由浅入深明白装饰器是什么。假定你拥有最基本的Python知识,本文阐述的东西可能对那些在工作中经常接触Python的人有很大的帮助。 1、函数(Functions) 在...

python实现扫描ip地址的小程序

python实现扫描ip地址的小程序,具体代码如下所示: import os,time import sys start_Time=int(time.time()) ip_True...

pytorch sampler对数据进行采样的实现

PyTorch中还单独提供了一个sampler模块,用来对数据进行采样。常用的有随机采样器:RandomSampler,当dataloader的shuffle参数为True时,系统会自动...

python的unittest测试类代码实例

nittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结...