python中类的继承,初始化,super()__init__()方法
2018-09-07 20:30
946 查看
每日星语:
“你知道年轻的时候穷最大的问题是什么吗?”
“什么”
### “以为所有得不到的一切都是穷的错。眼睛盯着遥远的地方,却看不到身边一朵花的美丽”
2018-09-07 20:30:30 星期五今天遇到一个问题,爬虫在定义网页规则时,因为有十几个站点来自于统一网站,刚开始考虑去复制一份,只需要修改web_site变量即可。但考虑到两点,其一,站点过多,复制麻烦,其二,也是最重要的一点,当网站结构变化时,修改维护起来过于冗余复杂,所以就使用了模板继承的办法!
刚开时circHeNan.py代码如下
# encoding: utf-8 from datetime import datetime from .base_stock import BaseStock class CircHeNan(BaseStock): def is_detail_url(self, dom): return dom("#ess_mailrightpane") def parse_detail_url(self, dom, params): web_site = "河南委员会" store_json = { "info:title": dom("#tab_content)").text(), "info:publish_time": dom("#tab_content > tbody:nth-child(1)").text(), "info:source": web_site, "info:author": "", "info:content": dom("#tab_content > tbody").outerHtml(), "info:tag": dom("head meta[name=\"keywords\"]").attr("content") or "", "info:dese": dom("head meta[name=\"description\"]").attr("content") or "", "info:crawl_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "info:url": params["info:url"], "info:channel": "", "info:laiyuan": web_site, "info:contain_image": "True" if dom("#tab_content > tbody")("img") else "False" } return store_json
由于该类继承与BaseStock
class BaseStock(object): def __init__(self): self.html_clean = html_clean.HTMLClean() self.html = "" self.web_site=""
考虑到BsaeStock为父类,当子类重写init时,子类就不会继承父类的init,所以需要对父类中init重写完整
将circ.py定义成模板# encoding: utf-8 import re from datetime import datetime from .base_stock import BaseStock from components import html_clean class Csrc(BaseStock): def __init__(self): self.html_clean = html_clean.HTMLClean() self.html = "" self.web_site = "" def is_detail_url(self, dom): return dom(".content") def parse_detail_url(self, dom, params): store_json = { "info:title": dom("#tab_content)").text(), "info:publish_time": dom("#tab_content > tbody:nth-child(1)").text(), "info:source":self.web_site, "info:author": "", "info:content": dom("#tab_content > tbody").outerHtml(), "info:tag": dom("head meta[name=\"keywords\"]").attr("content") or "", "info:dese": dom("head meta[name=\"description\"]").attr("content") or "", "info:crawl_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "info:url": params["info:url"], "info:channel": "", "info:laiyuan": self.web_site, "info:contain_image": "True" if dom("#tab_content > tbody")("img") else "False" } return store_json
修改circHeNan.py文件里的内容如下:
from .circ import Circ from components import html_clean class CircHeNan(Circ): def __init__(self): self.html_clean = html_clean.HTMLClean() self.html = "" self.web_site = "河南委员会"
这时候的circHeNan.py虽然只有很少的几行,但这时候的代码效果和刚开始的一样了
更新文章
2018-09-11 20:43:54 星期二
在多重类的继承过程中,其实存在这super().__init__()的方法,这个方法可以很好的解决冲突问题,上面的文掌可以不需要这么复杂,只需要用super()完美解决将模板文件circ.py修改如下
import re from datetime import datetime from .base_stock import BaseStock class Csrc(BaseStock): def __init__(self): super(Csrc,self).__init__() self.web_site = "" def is_detail_url(self, dom): return dom(".content") def parse_detail_url(self, dom, params): store_json = { "info:title": dom("#tab_content)").text(), "info:publish_time": dom("#tab_content > tbody:nth-child(1)").text(), "info:source":self.web_site, "info:author": "", "info:content": dom("#tab_content > tbody").outerHtml(), "info:tag": dom("head meta[name=\"keywords\"]").attr("content") or "", "info:dese": dom("head meta[name=\"description\"]").attr("content") or "", "info:crawl_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "info:url": params["info:url"], "info:channel": "", "info:laiyuan": self.web_site, "info:contain_image": "True" if dom("#tab_content > tbody")("img") else "False" } return store_json
而circHeNan.py的文件可以修改为
from .circ import Circ class CircHeNan(Circ): def __init__(self): super()__init__() self.web_site = "河南委员会"
简单案例可以参考这个这个页面
的实例部分,看懂了就理解了
相关文章推荐
- python 详解类class的继承、__init__初始化、super方法(五)
- Python中super()和__init__()方法
- Python中的__init__()方法及其继承
- python 子类继承父类的__init__方法
- JAVA第七节-继承,继承中方法重写,继承的初始化顺序,final关键字,super关键字,object类
- Python中新式类,子类使用super方法显式调用父类的__init__方法
- python中子类继承父类的__init__方法实例
- Python3基础 super层层调用父类的__init__方法 子类的__init__覆盖了父类的__init__的解决方法
- python学习——super()方法实现类的继承
- 【Python】Python3 多继承的super init()问题
- [置顶] python入门:类的多重继承问题、重写、未绑定的超类方法、super()
- Python 类继承,两种初始化(init)的区别
- java中的继承,继承中方法的重写,继承的初始化顺序,final关键字,super关键字
- Java第11次作业:什么是继承?继承的好处?什么是覆写?super()?构造代码块?子父类初始化顺序? 抽象类能用final声明吗?final关键字声明类 方法 变量以及全局常量?抽象类的构造方法?
- python继承和重写init方法--实例
- python继承,调用未绑定的父类方法,super,多级继承
- python继承和重写init方法
- OC第三天-继承,初始化方法,便利构造器 ,self,super,方法重写
- python中子类继承父类的__init__方法实例
- 6 python 类的初始化,方法,继承以及多态