zoukankan      html  css  js  c++  java
  • 老王Python-进阶篇4-面向对象第三节

    一: 写一个网页数据操作类。完成下面的功能:

    提示:需要用到urllib模块

    get_httpcode()获取网页的状态码,返回结果例如:200,301,404等 类型为int

    get_htmlcontent() 获取网页的内容。返回类型:str

    get_linknum()计算网页的链接数目。

     1 class get_web_data():
     2     
     3     def __init__(self,url):
     4         self.url=url
     5     
     6     def get_httpcode(self,):
     7         new_code=urllib.urlopen(self.url).code
     8         return new_code
     9     
    10     def get_htmlcontent(self):
    11         content_1=urllib.urlopen(self.url).read()
    12         return content_1
    13     
    14     def get_linknum(self):
    15         num=urllib.urlopen(self.url).read()
    16         return len(num.split('<a href='))-1
    17 
    18 if __name__=='__main__':
    19     new_url=get_web_data('http://www.cnblogs.com/duyaya/')
    20     
    21     print new_url.get_httpcode()
    22     print new_url.get_htmlcontent()
    23     print new_url.get_linknum()

    二:

    class SchoolMember:
    '''Represents any school member.'''
    def __init__(self, name, age):
    self.name = name
    self.age = age
    print '(Initialized SchoolMember: %s)' % self.name

    def tell(self):
    '''Tell my details.'''
    print 'Name:"%s" Age:"%s"' % (self.name, self.age),

    class Teacher(SchoolMember):
    '''Represents a teacher.'''
    def __init__(self, name, age, salary):
    SchoolMember.__init__(self, name, age)
    self.salary = salary
    print '(Initialized Teacher: %s)' % self.name

    def tell(self):
    print 'Salary: "%d"' % self.salary

    class Student(SchoolMember):
    '''Represents a student.'''
    def __init__(self, name, age, marks):
    SchoolMember.__init__(self, name, age)
    self.marks = marks
    print '(Initialized Student: %s)' % self.name

    def tell(self):
    print 'Marks: "%d"' % self.marks

    t = Teacher('Mrs. Shrividya', 40, 30000)
    s = Student('Swaroop', 22, 75)

    members = [t, s]
    for member in members:
    member.tell()

    体会下这段代码,把结果的执行流程用自己的话写下。

  • 相关阅读:
    事务的隔离级别
    常用linux命令
    cpu.load avg和cpu使用率的区别
    【Leetcode】55. Jump Game
    【Leetcode】322. coin-change
    34.find-first-and-last-position-of-element-in-sorted-array
    【LeetCode】56.merge-intervals
    Leetcode】210.course-schedule-ii
    基于Thread实现自己的定时器Timer
    Boost--内存管理--(1)智能指针
  • 原文地址:https://www.cnblogs.com/duyaya/p/8196319.html
Copyright © 2011-2022 走看看