python中类的继承,初始化,super()__init__()方法

核能气质少年 提交于 2019-12-18 17:43:09

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

每日星语:

“你知道年轻的时候穷最大的问题是什么吗?”

“什么”

### “以为所有得不到的一切都是穷的错。眼睛盯着遥远的地方,却看不到身边一朵花的美丽”

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 = "河南委员会"

简单案例可以参考这个这个页面 的实例部分,看懂了就理解了

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!