For ๋ฐ๋ณต๋ฌธ(Loop) ์ดํดํ๊ธฐ
- ํํ๋ก ์์ฑ๋ ์น์ฌ์ดํธ๋ค์ด ๋์ ์ค์ธ์ง, ์๋์ง ํ์ธํฉ๋๋ค.
- ํํ ์์ ์๋ ๊ฐ๊ฐ์ ์์ดํ
์ ์ฌ์ฉํด์ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
- tuple์ ๊ฐ๊ฐ์ item์ ์ฝ๋๋ฅผ ์คํ์ํต๋๋ค.
- tuple๋ฟ๋ง ์๋๋ผ, list๋ ๋ง์ฐฌ๊ฐ์ง๋ก for ๋ฐ๋ณต๋ฌธ์ ๋์ผํ๊ฒ ์๋ํฉ๋๋ค.
- ํ์ฌ ์ฒ๋ฆฌ์ค์ธ item์ด ๋ฌด์์ธ์ง ์ ์ ์์ต๋๋ค.
- for๋ฌธ์ ๊ฐ๊ฐ์ item์ด ์คํ๋ ๋, placeholder๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
- placeholer์ ์ด๋ฆ์ ์ํ๋๋๋ก ์์ฑํ ์ ์์ต๋๋ค.
- tuple์ธ websites ๋ฆฌ์คํธ์ ๋ชจ๋ item์ ๋ํด ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
- ํ์ด์ฌ์ each๋ก ํํ์ item์ ์ ๊ทผํ ์ ์๊ฒ ํด์ค๋๋ค.
- ์ด๋ค ์งํฉ์ฒด๊ฐ ์์ด๋, ํ์ฌ ์ฒ๋ฆฌ์ค์ธ ๋ด๋ถ ์์ดํ
์ ์ ๊ทผํ ์ ์์ต๋๋ค.
- ๋ค์ด๋ฐ ๊ด์ต ์ค ํ๋, tuple์ด๋ list๋ฅผ ๋ง๋ค๋ ๋ณต์ํ์ ์ฌ์ฉํฉ๋๋ค.
- ex) websites - website
- for ๋ฐ๋ณต๋ฌธ์ sequence์์ ๊ฐ item์ผ๋ก ์ฝ๋๋ฅผ ์คํ์ํฌ ์ ์๋ ๋ฐฉ๋ฒ์
๋๋ค.
websites = (
"google.com",
"aribnb.com",
"twitter.com",
"facebook.com"
)
# for ๋ฐ๋ณต๋ฌธ
for website in websites:
print("hello")
# hello
# hello
# hello
# hello
# for ๋ฐ๋ณต๋ฌธ > each๋ผ๋ placeholder๋ฅผ ์ฌ์ฉ
for website in websites:
print("each is equals to", each)
# each is equals to google.com
# each is equals to airbnb.com
# each is equals to twitter.com
# each is equals to facebook.com
- ํํ ๋ด์ ์๋ ์น์ฌ์ดํธ ์ฃผ์๋ ๊ทธ์์ฒด๋ง์ผ๋ก๋ ์ฌ์ดํธ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
- http ๋๋ https์ ://๋ฅผ ๋ถ์ธ ์น์ฌ์ดํธ๋ก๋ง ์ฃผ์์ด๋์ด ๊ฐ๋ฅํฉ๋๋ค.
- ๋ง์ฝ, ์น์ฌ์ดํธ ์ฃผ์๊ฐ https๋ก ์์ํ๋ฉด ๋ฐ๋ก ์ด๋ํ๊ณ ,
- ์๋๋ฉด ์์ https๋ฅผ ๋ถ์
๋๋ค.
- https://์ ์ ๋ฌด๋ฅผ ๋ฐ์ ธ์ if๋ฌธ ์์์ method๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- f"{๋ณ์}"๋ฅผ ์ฌ์ฉํ์ฌ, ๋ณ์์ ํ
์คํธ๋ฅผ ํจ๊ป ์ฌ์ฉํฉ๋๋ค.
- string์์ ๋ณ์๋ฅผ ๋ฃ๋ ๋ฐฉ๋ฒ f"{๋ณ์}"
- if๋ฌธ์ True์ธ์ง False์ธ์ง ํ๋จํ๋ฉฐ,
startwith ๋ฉ์๋๋ Boolan์ ์ค๋๋ค.
- if not์ ์ฌ์ฉํฉ๋๋ค.
websites = (
"google.com",
"airbnb.com",
"https://twitter.com",
"facebook.com",
"https://tiktok.com"
)
for website in websites:
if not website.startswith("https://"):
print(f"https://{website}")
else:
print(website)
- the python standard library์
- ์ฌ์ฉํ ์ ์๋ module๊ณผ function์ด ๋ฌธ์ํ๋์ด์์ต๋๋ค.
- ์คํ ๋ค๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ธฐ๋ณธ์ ํฌํจ๋์ด์์ง ์์ ๋ชจ๋์ ์ฌ์ฉํฉ๋๋ค.
- ๊ธฐ๋ณธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ ๋ค๋ฅธ ์ฌ๋๋ค์ด ๋ง๋ module์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ๋ค์ํ ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ pypi๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- pypi๋ ๋ค๋ฅธ ์ฌ๋์ด ๋ง๋ project๋ module์ ๋ชจ์๋ ๊ณณ์
๋๋ค.
- pypi์์ ๊ฒ์ํ Requests ํจํค์ง๋ ์ฌ์ฉ์์ ์ฝ๋์์
- ์น์ฌ์ดํธ๋ก request๋ณด๋ด๊ฒ ํด์ค๋๋ค.
- requests ํจํค์ง์์ getํจ์๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉํฉ๋๋ค.
- getํจ์๋ ์ด๋ํ ํ, website๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
from requests import get
- getํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- website๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ ๋์ , getํจ์๋ฅผ ์คํํฉ๋๋ค.
- getํจ์๋ response๋ฅผ returnํฉ๋๋ค.
- reponse๋ ์น์ฌ์ดํธ ์์ฒญ์ ๋ํ ์๋ฒ์ ์๋ต์ ๋งํฉ๋๋ค.
- ์ด ํจ์๊ฐ return(response)๊ฐ์ด ์๊ธฐ๋๋ฌธ์, ๊ทธ ๊ฐ์ ๊ฐ์ ธ์ต๋๋ค.
- return๊ฐ์ด๋, ํจ์๊ฐ ์ด๋ค ์์
์ ์ํํ๊ณ ,
- ์์
์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ผ๋ก ๋๋ ค์ฃผ๋ ๊ฒ์ ๋งํฉ๋๋ค.
- ๊ทธ ๋ฆฌํด๊ฐ์ response๋ผ๋ ๋ณ์๋ก ๋ฐ์ต๋๋ค.
- ์ธํฐ๋ท์ HTTP protocol์ ๊ธฐ๋ฐํฉ๋๋ค.
- HTTP protocal์ด๋, https://์ ๋งํฉ๋๋ค.
- ์ปดํจํฐ๋ค์ด ์๋ก ์ํตํ๋ ๋ฐฉ์์ HTTP request์
๋๋ค.
- ๊ทธ requset๊ฐ ์ ์์ธ์ง ์๋์ง ํ์ธํ ์ ์๋ ์๋จ์ด ํ์ํฉ๋๋ค.
- request์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๋ ๋ฐฉ๋ฒ์ผ๋ก HTTP์ฝ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- 100๋ฒ๋
- 200๋ฒ๋ -> OK / ์์ฒญ์ด ์ฑ๊ณต๋์์ต๋๋ค.
- 300๋ฒ๋ -> redirection
- 400๋ฒ๋ -> Error / ์๋ฒ๊ฐ ์์ฒญ์ ์ดํดํ ์ ์์ต๋๋ค.
- 500๋ฒ๋
- ์์ฒญ์ ๋ํ ๋ฉ์๋ ์ค status_code๋ฅผ ์ฌ์ฉํ์ฌ ์ํ์ฝ๋ ๋๋ฒ๋ง ์ถ๋ ฅํฉ๋๋ค.
- ๋ฐ๋ ์์ฒญ ์ํ์ฝ๋๋ฅผ ์กฐ๊ฑด๋ณ๋ก ์คํ๋ฌธ์ ์์ฑํฉ๋๋ค.
from requests import get
websites = (
"google.com",
"airbnb.com",
"https://twitter.com",
"facebook.com",
"https://tiktok.com"
)
for website in websites:
if not website.startswith("https://"):
website = f"https://{website}"
response = get(website)
print(response)
"""
<Response [200]>
<Response [200]>
<Response [200]>
<Response [200]>
<Response [200]>
"""
from requests import get
websites = (
"google.com",
"airbnb.com",
"https://twitter.com",
"facebook.com",
"https://tiktok.com"
)
for website in websites:
if not website.startswith("https://"):
website = f"https://{website}"
response = get(website)
if response.status_code == 200:
print(f"{website} is OK")
else:
print(f"{website} is not OK")
"""
https://google.com is OK
https://airbnb.com is OK
https://twitter.com is OK
https://facebook.com is OK
https://tiktok.com is OK
"""
- results๋ผ๋ dictionary๋ฅผ ๋ง๋ญ๋๋ค.
- results ๋์
๋๋ฆฌ ์์ ์๋ก์ด entry๋ฅผ ๋ง๋ญ๋๋ค.
- ์ฌ๊ธฐ์ ์๋ก์ด key๋ฅผ dictionary์์ ๋ง๋ญ๋๋ค.
from requests import get
websites = (
"google.com",
"airbnb.com",
"https://twitter.com",
"facebook.com",
"https://tiktok.com"
)
results = {}
for website in websites:
if not website.startswith("https://"):
website = f"https://{website}"
response = get(website)
if response.status_code == 200:
results[website] = "OK"
else:
results[website] = "FAILED"
print(results)
"""
{'https://google.com': 'OK',
'https://airbnb.com': 'OK',
'https://twitter.com': 'OK',
'https://facebook.com': 'OK',
'https://tiktok.com': 'OK'}
"""
-