๋ชจ๋์ ํ์์ฑ
์ฝ๋์ ๊ธธ์ด๊ฐ ๊ธธ์ด์ง๋ฉด ๋ชจ๋ ํจ์, ๋ณ์ ๊ตฌํ์ ๋ถ๊ฐ๋ฅ>>๋๊ตฐ๊ฐ ๋ง๋ค์ด๋์ ํจ์, ๋ณ์๋ฑ์ ํ์ฉํ์.
๋ชจ๋: ํน์ ๋ชฉ์ ์ ๊ฐ์ง ํจ์, ์๋ฃ์ ๋ชจ์
๋ชจ๋ ์ฌ์ฉํ๊ธฐ
import random #random ๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
๋ชจ๋์ ์ข
๋ฅ
random.randrange(start,stop)
ํจํค์ง
๋ชจ๋์ ํด๋(Directory)๋ก ๊ตฌ๋ถํ์ฌ ๊ด๋ฆฌํ๋ ๊ฒ
ํจํค์ง ์ ๋ชจ๋ ์ฌ์ฉํ๊ธฐ
ํด๋ user - ๋ชจ๋ cal - ํจ์plus()
1) import ์ด์ฉ
import user.cal #user ํด๋ ์ cal ๋ชจ๋ ๋ถ๋ฌ์๋ผ
print(cal.plus(3,4))
2) from ํด๋.๋ชจ๋ import ํจ์
ํจ์ ๋ณ์ ์ฌ์ฉ์ .์์จ๋๋จ
from user.cal import plus
print(plus(3,4)) #cal.puls๋ผ๊ณ ์์จ๋๋จ
urlopen() : ์ด ํจ์์ url์ ๋ฃ์ผ๋ฉด ํด๋น url์ ์ ๊ทผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
read() : ๊ฒฐ๊ณผ๋ฅผ ํด๋
ํ์ฌ ๋ฌธ์์ด๋ก ๋ฐํํฉ๋๋ค.
decode() : ๋ฌธ์์ด์ ํน์ ์ธ์ฝ๋ฉ ๋ฐฉ์์ผ๋ก ํด๋
ํฉ๋๋ค. (์ ๋์ฝ๋, ์์คํค ์ฝ๋ ๋ฑ)
์ค์ต: ์นํ์ด์ง ์คํ
- from-import๋ฅผ ์ด์ฉํด์ urllibํจํค์ง ์ request ๋ชจ๋์์ urlopen ํจ์๋ฅผ ๋ถ๋ฌ์ค๋ ์ฝ๋๋ฅผ ์์ฑํด๋ด ์๋ค.
- ๋ค์ ์ฃผ์๋ฅผ urlopenํ๊ณ , read() ํ ๋ค์, ์ด๋ฅผ utf-8์ผ๋ก decode ํ ๊ฒฐ๊ณผ๋ฅผ ๋ณ์ webpage์ ๋ฃ์ด๋ด ์๋ค. https://en.wikipedia.org/wiki/Lorem_ipsum
- ๋ณ์ webpage๋ฅผ ์ถ๋ ฅํด๋ด ์๋ค. ๋ฌด์์ด ๋์ค๋์?
## ์ด๋ ๊ฒ ํด๋ณด์ธ์!๋ฅผ ๋ฐ๋ผ ์ํํด๋ณด์ธ์.
from urllib.request import urlopen
webpage = urlopen("https://en.wikipedia.org/wiki/Lorem_ipsum").read().decode("utf-8")
#ํจ์๋ A().B().C() ์ ๊ฐ์ด ์ฐ์์ ์ผ๋ก ์ฌ์ฉ๊ฐ๋ฅ
print(webpage)
๋๊ธ