c = """
        <div class="select_div">
            <span data-i18n="footer.relationSites">연관사이트</span>
            <ul>


            </ul>
        </div>
        <ul class="list_sns">

페이스북"></a></li>

블로그"></a></li>

유트브"></a></li>
        </ul>
"""

# 키워드 검색
print(c.find("list_sns"))

# 키워드를 이용한 스키핑
c = c[167:] # 필요없는 데이터 날리기
print(c)

# <li> 태그를 기준으로 쪼개기
c = c.split("</li>")
print(c)

print("-----------------------------------------------------------")
# c[0] 데이터 출력
print(c[0])

# 각 태그에서 usrl만 출력
url = c[0].split('" target')[0]
print(url)

# 각 태그에서 usrl만 출력 2
url = url.split('href="')[1]
print(url)

print("------------------------------------------------------------")
#
print(c[0])

#
name = c[0].split('alt="')[1]
print(name)

name = name.split('"')[0]
print(name)

print()


for i in range(len(c)-1):
    url = c[i].split(' "target')[0]
    url = url.split('href="')[1]
    print(url)
    name = c[i].split('alt="')[1]
    name = name.split('"')[0]
    print(name)
    

 

 

# 정규표현식
import re

c = """
        <div class="select_div">
            <span data-i18n="footer.relationSites">연관사이트</span>
            <ul>


            </ul>
        </div>
        <ul class="list_sns">

페이스북"></a></li>

블로그"></a></li>

유트브"></a></li>
        </ul>
"""

pattern = re.compile('https://.*" target')
result = pattern.search(c)
print(result.group())

pattern = re.compile('[a-z:/.]*')
result = pattern.search(result.group())
print(result.group())

 

 

class StaticTest :

    num = 3 # 클래스 프로퍼티

    # 단점
    def __init__(self, aaa):
        self.aaa = aaa

    @staticmethod
    def add(x, y):
        return x + y

    @staticmethod
    def min(x):
        # return x - self.aaa # 인스턴스의 프로퍼티(속성)에 접근을 불허한다; 내부 프로퍼티를 사용할 경우에는 staticmethod의 사용은 안된다.
        pass

    @classmethod
    def class_info(cls):
        print(cls)
        print(cls.num) # 클래스메서드는 클래스 프로퍼티를 가져올 수 있다.

    @classmethod
    def class_info2(cls,x):
        return cls.num

# 인스턴스를 생성할 필요가 없음
print(StaticTest.add(1,2))
StaticTest.class_info()


 

class Country:

    name = "국가명"
    population = "인구"
    capital = "수도"

    @classmethod
    def show(cls):
        print("{} {} {}".format(cls.name, cls.population, cls.capital))



class Korea(Country): # Country 클래스에 있는 모든 기능을 쓰겠다.

    @classmethod
    def introduceCtr(cls):
        print("어서오세요, 대한민국에.")


    @classmethod
    def set_name(cls, name):
        cls.name = name

b = Country() # 부모클래스
b.show()

a = Korea() # 자식클래스
a.set_name("대한민국")
a.introduceCtr()
a.show()

 

 

class Parent1:

    def __init__(self, a):
        self.a = a

    def show(self):
        print(self.a)


class Child1(Parent1):

    def show_a_plus(self):
        print("자식클래스에서 호출한 부모클래스 속성 : {}".format(self.a))

a = Child1(10) # init 메서드 호출 가능. 대신에 하위 값들에 대해서 강제성을 가진다.
a.show_a_plus()


 

 

# 오버라이딩.

# 덮어씌우기. 부모클래스의 기능을 자식클래스의 기능으로 덮어씌우는 기능.

# 부모클래스
class Machine1:

    def __init__(self, a, b):
        self.a = a
        self.b = b

    def show(self):
        print("{} : {}".format(self.a, self.b))


# 자식클래스
class Machine2(Machine1): # Machine1을 상속받은 Machine2

    # 기능 고치기
    # 동일한 이름으로 함수를 생성
    # 완전히 새로운 기능으로 출력시켜줌.
    def show(self):
        print("자식메서드의 기능 {} : {}".format(self.a, self.b)) # 부모자식간의 변수는 바뀔 수 없다.

a = Machine2("abc", 1234)
a.show() # 부모에 있는 기능이 오버라이딩 (덮어씌우기) 됐다.





'Dev_Python' 카테고리의 다른 글

[Python] decorator  (0) 2023.09.21
[Python] property 예제  (0) 2023.09.21
[Python] del 예제  (0) 2023.09.21
[Python] init 예제  (0) 2023.09.21
[Python] 클래스 - 접근제어 예제  (0) 2023.09.21
계속 까먹네;