728x90
반응형

6001 : [기초-출력] 출력하기01(설명)(py)

시간 제한: 1 Sec  메모리 제한: 128 MB

 

 

문제 설명

 

python 언어에서 가장 기본적인 명령이 출력문이다.
print( )를 이용해 다음 단어를 출력하시오.

Hello

 

입력

 

 

출력

 

Hello

 

내 소스

 

print("Hello")

 

모범 답안

 

print("Hello")
728x90
반응형
728x90
반응형

6003 : [기초-출력] 출력하기03(설명)(py) 

시간 제한: 1 Sec  메모리 제한: 128 MB

 

 

문제 설명

 

이번에는 줄을 바꿔 출력하는 출력문을 연습해보자.
다음과 같이 줄을 바꿔 출력해야 한다.

Hello
World
(두 줄에 걸쳐 줄을 바꿔 출력)

 

입력

 

입력 없음

 

출력

 

Hello
World

 

내 소스

 

print("Hello")
print("World")

 

모범 답안

 

print("Hello")
print("World")

'''
또는

print("Hello\nWorld")
'''
728x90
반응형
728x90
반응형

 

* 본 포스팅은 주피터 노트북에서 진행하였다.

 

!pip install matplotlib
import matplotlib.pyplot as plt
data = [1,2,3,4,3,2,1]
plt.figure('그래프 이름')
plt.plot(data)
plt.show()

 

import matplotlib.pyplot as plt
x = [10, 20, 30, 40, 50, 60, 70]
y = [1,2,3,4,5,2,1]
plt.figure('그래프 이름')
plt.plot(x,y)
plt.show()

 

 

import matplotlib.pyplot as plt
import numpy as np
time = np.arange(0,10,0.01)
y = np.sin(time)
plt.figure('그래프 이름')
plt.plot(time,y)
plt.show()

 

이 코드의 arange는 0부터 10까지 0.01간격으로 등분

 

 

import matplotlib.pyplot as plt
import numpy as np
time = np.arange(0,10,0.01)
sin_y = np.sin(time)
cos_y = np.cos(time)
plt.figure('그래프 이름')
plt.plot(time,sin_y)
plt.plot(time,cos_y)
plt.show()

 

import matplotlib.pyplot as plt
import numpy as np
time = np.arange(0,10,0.01)
sin_y = np.sin(time)
cos_y = np.cos(time)
plt.figure('sin, cos 그래프')
plt.plot(time,sin_y, label='sin')
plt.plot(time,cos_y,label='cos')
plt.legend() #범례
plt.xlabel('time')
plt.ylabel('value')
plt.title('sin, cos Grapeh') #그래프 이름
plt.grid() #그리드 설정
plt.show()

 

import matplotlib.pyplot as plt #막대 그래프
import numpy as np
data=[10,20,30,55]
x=[0,1,2,3]
plt.bar(x,data,width=0.3)
plt.show()

 

가로형 막대 랜덤 으로 생기기

 

1.

import matplotlib.pyplot as plt #막대 그래프
import numpy as np
y_data = []
x_data = []
for i in range(6):
    x_data.append(i)
    y_data.append(np.random.rand()*20)
plt.bar(x_data, y_data,width=0.5)
plt.show()

 

2.

import matplotlib.pyplot as plt #막대 그래프
import numpy as np
a = np.random.random(4)
data = np.random.random(4)
plt.bar(a,data,width=0.1)

 

가로형 막대 그리기

import matplotlib.pyplot as plt #막대 그래프
import numpy as np
data=[10,20,30,5]
x=[1,2,3,4]
plt.barh(x,data,height=0.3)
plt.show()

 

원형 그래프 그리기

import matplotlib.pyplot as plt #막대 그래프
import numpy as np
data=[10,20,30,5]
plt.pie(data)
plt.show()

728x90
반응형
728x90
반응형

 

2021.06.09 - [Data Analysis/web crawling] - [Crawling] Slack Bot 만들기

 

전 포스팅과 연결되어 있다. 이번에는 슬랙에서 봇채팅 치는 것을 하겠다!

 

슬랙에 들어가 왼쪽 편을 봐보자 

 

아 그리고 나는 test라는 채널을 추가했다.

 

 

세로로 나열된 점 세개를 누른다.

 

 

앱을 누른다.

 

hello를 눌러주면 

 

여기에 내가 만든 봇이 뜨는데 만약 안 뜨더라도 검색하면 뜰 것이다.

 

이렇게 뜨고 동그라미로 표시한 부분을 눌러준다.

 

 

이 앱을 채널에 추가 를 눌러 원하는 채널에 초대해준다.

 

나는 test 채널에 초대할 것이다.

 

 

 

추가를 누르고 test 채널에 가보면 이게 뜰 것이다.

 

이제 파이썬으로 채팅을 해볼 것이다.

 

나는 주피터 노트북을 이용했는데 상관없다.!

 

from slackclient import SlackClient
import requests as rq
from bs4 import BeautifulSoup
import time

slack_token = 'xoxb-460297928240-2145122313062-cafL1R4QG7nFNRn4QMIrJeiR' #발급 받은 토큰
sc = SlackClient(slack_token)
    
    #메세지 전달
def notification(message):
    sc.api_call(
        "chat.postMessage",
        channel="#test", #{#채널}의 형태로 채널 지정
        text=message
    )

 

발급받은 토큰과 채널 이름은 사람마다 다를 것이다. 바꿔준 후

 

notification('안녕')

 

그전에 만들어둔 봇이 떴다..! 

 

 

728x90
반응형
728x90
반응형

* 본 포스팅은 주피터 노트북에서 진행하였다.

 

[ 용어 정리 ]

크롤링(crawling) 혹은 스크레이핑(scraping)은 웹 페이지를 그대로 가져와서 거기서 데이터를 추출해 내는 행위다. 크롤링하는 소프트웨어는 크롤러(crawler)라고 부른다.

 

requests와 bs4를 이용하여 크롤러를 만들 수 있지만 자바스크립트의 동작이 많은 웹 사이트일 경우 한계가 있기 때문에 selenium을 사용한다.

 

chrome driver가 필요한데 만약 설치가 안되어 있다면 아래 포스팅을 참고하면 좋을 것이다.

 

2021.06.09 - [Data Analysis/web crawling] - [Crawling] 크롬 드라이버(ChromeDriver) 설치하기

 

[Crawling] 크롬 드라이버(ChromeDriver) 설치하기

chrome driver를 설치한다. https://chromedriver.chromium.org/downloads ChromeDriver - WebDriver for Chrome - Downloads Current Releases If you are using Chrome version 91, please download ChromeDrive..

hello-ming.tistory.com

 

from selenium import webdriver
url = "https://pjt3591oo.github.io"
driver = webdriver.Chrome('chromedriver')
driver.get(url)

브라우저가 자동으로 열린다.

 

만약 selenium이 없다면 아래의 코드를 이용하면 된다.

!pip install selenium

 

요소를 가져오자

selected_id = driver.find_element_by_id('nav-trigger') #id로 요소 선택
print(selected_id)
print(selected_id.tag_name) #태그명
print(selected_id.text) #태그에 속한 문자

 

selected_p = driver.find_element_by_tag_name('p') #id로 요소 선택
print(selected_p)
print(selected_p.tag_name) #태그명
print(selected_p.text) #태그에 속한 문자

 

selected_tags_p = driver.find_elements_by_tag_name('p') #id로 요소 선택
print(selected_tags_p)

 

selected_link = driver.find_element_by_class_name('p')
print(selected_link)
print(selected_link.tag_name)
print(selected_link.text)

 

 

728x90
반응형
728x90
반응형

 

* 본 포스팅은 주피터 노트북에서 진행하였다.

 

def test_generator():
    yield 1
    yield 2
    yield 3
gen = test_generator()
type(gen)

 

next(gen) #1
next(gen) #2
for i in test_generator():
    print(i)

 

def test_generator():
    print('yield 1 전')
    yield 1
    print('yield 1 과 2 사이')
    yield 2
    print('yield 2 과 3 사이')
    yield 3
    print('yield 3 후')
for i in test_generator():
    print(i)

 

yiled를 만나면 반환되지만 내용은 유지가 된다. 양보느낌

 

yiled를 보면 generator라고 생각하면 된다.

 

무한으로 generator 생성하기

def infinite_generator():
    count=0
    while True:
        count+=1
        yield count
        
gen = infinite_generator()
next(gen)

계속 누를 수록 숫자가 증가한다.

 

우리가 알고있는 리스트, Set, Dictionary의 표현식의 내부도 사실 generator 이다.

 

 

[x *x for x in [2,4,6]]
#[4, 16, 36]
print(type(x*x for x in [2,4,6]))
#<class 'generator'>

자바스크립트의 yield와 비교해보기

visual Studio Code 에서 진행하였다.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        function* idMaker(){ //자바스크립트 generator
            var index=0;
            shile(index<3)
                yield index++
        }
        var gen = idMaker();
        console.log(gen.next().value)
        console.log(gen.next().value)
        console.log(gen.next().value)
    </script>
    <title>Document</title>
</head>
<body>
    
</body>
</html>

 

 

728x90
반응형
728x90
반응형
class IterClass(object):
    def __init__(self,start,last):
        self.currnet = start
        self.max = last
    
    def __iter__(self): #없으면 'object is not iterable' 예외 발생
        return self
    
    def __next__(self):
        if self.currnet > self.max: #current의 값이 __next__ 호출시 마다 1씩 증가되고 10이 되면 여기 if문에 도달하여 예외 발생됨
            raise StopIteration
        else:
            self.currnet += 1
            return self.currnet -1 #보여주기

 

n_list1 = IterClass(1,10)
type(n_list1)

 

n_list1.__next__() #1
n_list1.__next__() #2
n_list1.__next__() #3

 

for 문으로 바꾸면

 

for i in range(0,10):
    print(n_list1.__next__())

 

배열이나 tuple list는 iterable 가능한 객체이다.

 

내부적으로자동으로 advancedfor 같은 구문에서 next가 자동으로 호출된다.

 

마지막에 도달하면 종료되는 것이다.

 

 

728x90
반응형
728x90
반응형

 

* 본 포스팅은 주피터 노트북에서 진행하였다.

from bs4 import BeautifulSoup
html = """<html> <head><title class="t" id="ti">test site</title></head> <body> <p>test</p> <p>test1</p> <p>test2</p> </body></html>"""
soup = BeautifulSoup(html,'lxml')
tag_title = soup.title
print(tag_title['class'])

tag_title.get('class') #get으로 class의 속성을 가져와라

#둘다 같은 결과

만약 오류가 뜬다면 이대로 입력해주자! 주피터 노트북에서는 코드 앞에 !을 붙이면 된다. cmd로 할 경우 !을 빼면 된다.

 

!pip install beautifulsoup4

!pip install lxml

 

tag_title.attrs #attribute

 

이 둘의 차이는 

 

tag_title.get('class1') #속성이 없는 클래스 호출할 때 get은 오류안뜸

tag_title['class1'] #오류뜸

 

tag_title.get('class1',default="hi") #값이 없을 때

 

data_text = tag_title.text
data_text

data_text = tag_title.string
data_text

같은 값을 가져오지만 타입이 다르다

 

data_text = tag_title.text
data_string = tag_title.sring
print("text : ",data_text, type(data_text))
print('string : ',data_string, type(data_string))

 

tag_p = soup.p
tag_p

 

data_text = tag_p.text
data_string = tag_p.string
print('text : ',data_text, type(data_text))
print('string : ',data_string, type(data_string))

 

html = """<html> <head><title>test site</title></head> <body> <p><span>test1</span><span>test2</span></p> </body></html>"""
soup = BeautifulSoup(html,'lxml')
tag_p = soup.p
tag_p

 

data_text = tag_p.text
data_string = tag_p.string
print('text : ',data_text, type(data_text))
print('string : ',data_string, type(data_string))

 

조건문을 활용하여 데이터를 확인할 수 있다. 아래의 코드는 span태그가 있는지의 여부를 묻는다.

if tag_p.span.string !=None:
    print('있다')

 

contents 속성과 children 속성을 이용하여 자식태그를 가져올 수 있다.

 

contents 속성을 이용하여 list 형태로 자식 태그를 가져온다.

tag_p_children = soup.p.contents
print(tag_p_children)

 


tag_p_children = soup.p.children
tag_p_children #iterate

 

 

문제 ) 반복문을 이용하여 둘다 출력하기

예시

a_tuple = (1,2,3)
b_iterator = iter(a_tuple)
print(b_iterator.__next__())
print(b_iterator.__next__())
print(b_iterator.__next__())

 

이것을 응용하자

 

tag_p_contents = soup.p.contents
tag_p_contents #[<span>test1</span>, <span>test2</span>]

tag_p_children = soup.p.children
tag_p_children # <list_iterator at 0x243f02ea220>
for i in tag_p_contents:
    print(i)

for i in tag_p_children:
    print(i)

728x90
반응형
728x90
반응형

 

* 본 포스팅은 주피터 노트북에서 진행되었다.

 

url = "https://hello-ming.tistory.com/"
res=rq.get(url,params={"key":"홍길동","key1":"홍말자","key2":"김개똥"})
res.url #한글은 꺠진다.

 

res=rq.get(url,params={"key":"hong","key1":"malga","key2":"hi"})
res.url

 

url = "https://hello-ming.tistory.com//?key=hong&key1=malga"
res.url #위와 같은방식인데 이거는 오타날 확률이 있음

 

Post로 데이터 보내기

 

url = "https://hello-ming.tistory.com/"
res=rq.post(url, data={"key1":"hong","key2":"icebear"})
res.url #post로 body에  싣어 보냄

dict1 = {"key1":"hong","key2":"icebear"}
import json
json.dumps(dict1) # '{"key1": "hong", "key2": "icebear"}'
str(dict1) # '{"key1": "hong", "key2": "icebear"}'

둘다 문자열 형태로 감 json 형태 유지의 차이

 

from urllib.request import urlopen,Request
req = Request(url)
page = urlopen(req)
page

 

 

728x90
반응형
728x90
반응형

https://devbruce.github.io/python/py-17-decorator/

 

[Python] Decorator (1)

 

devbruce.github.io

 

위 사이트에 Decorator의 설명가 예제가 잘 되어있어

 

사이트에 있는 예제를 기반으로 공부하고 내가 추가로 함수를 만들어 보며 Decorator에 대해 알아보았다!

 

Decorator란

  • 어떤 함수를 받아 명령을 추가한 뒤 이를 다시 함수의 형태로 반환하는 함수.
  • 어떤 함수의 내부를 수정하지 않고 기능에 변화를 주고 싶을 때 사용한다.
  • 함수를 꾸며준다.

자바의 Closure와 비슷하다.

 

Decorator의 구조

def 데코레이터이름(func):  # 기능을 추가할 함수를 인자로 받아온다.
    def 내부함수이름(*args, **kwargs):
        기존 함수에 추가할 명령
        return func(*args, **kwargs)
    return 내부함수이름

 

사이트에 있는 예제로 Decorator에 대해 이해해 보았다.

 

def test(a,b):
    print("계산 시작합니다.")
    print(a+b)
    print("계산 완료되었습니다.")
def test2(a,b):
    print("계산 시작합니다.")
    print(a*b)
    print("계산 완료되었습니다.")
def test3(a,b):
    print("계산 시작합니다.")
    print(a//b)
    print("계산 완료되었습니다.")

 

 print문이 매우 노가다이므로 이걸 Decorator를 사용한다!

 

def deco_func(origin_func):
    def nested_func(*args, **kwargs):
        print("계산 시작합니다.")
        origin_func(*args,**kwargs) #여기 내부함수 nested_func 함수에서 origin_func
        print("계산 완료되었습니다.")
        return
    return nested_func

 

  • 구조를 이해하기 위해서는 Closure 에 대해 알아야 하는데
클로저의 3가지 조건
1. 외부함수에서 내부함수를 반환
2. 함수가 중첩(외부함수 내부함수가 있어야한다)
3. 내부함수에서 외부함수의 지역변수를 참조하고 있어야 한다.

 

배우다 보니 스프링의 AOP 기능과 비슷하다고 느꼈다.

 

@deco_func
def test(a,b):
    print(a+b)
@deco_func
def test2(a,b):
    print(a*b)
@deco_func
def test3(a,b):
    print(a//b)

 

Decorator한 함수를 실행시켜 보니

 

test(5,2)
print("-"*30)
test2(5,2)
print("-"*30)
test3(5,2)

 

신기하다.

 

def new_func(a,b,c,d,e):
    print(a,b,c,d,e)

 

데코레이터가 적용되는 과정은 결국 클로저 활용과 동일하다.

 

deco_func(new_func)(1,2,3,100,200)

 

def a(**u):
    for i in u:
        print(i)
a(t=6, q=8) #키와 벨류로 전달

728x90
반응형

+ Recent posts