๐Ÿ ํŒŒ์ด์ฌ ์ฑŒ๋ฆฐ์ง€ DAY+06

yhwaยท2023๋…„ 2์›” 18์ผ
0
post-thumbnail

๐Ÿ“Python์œผ๋กœ ์›น ์Šคํฌ๋ž˜ํผ ๋งŒ๋“ค๊ธฐ: From #4.5 to #4.9

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

URL Formatting

- ํŠœํ”Œ ๋‚ด์— ์žˆ๋Š” ์›น์‚ฌ์ดํŠธ ์ฃผ์†Œ๋Š” ๊ทธ์ž์ฒด๋งŒ์œผ๋กœ๋Š” ์‚ฌ์ดํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
- 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)

์›น์‚ฌ์ดํŠธ์— Request ๋ณด๋‚ด๊ธฐ

- the python standard library์— 
- ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” module๊ณผ function์ด ๋ฌธ์„œํ™”๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
- ์Šคํƒ ๋‹ค๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๊ธฐ๋ณธ์„ ํฌํ•จ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
- ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ๋งŒ๋“  module์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
- ๋‹ค์–‘ํ•œ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” pypi๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
- pypi๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋งŒ๋“  project๋‚˜ module์„ ๋ชจ์•„๋‘” ๊ณณ์ž…๋‹ˆ๋‹ค.
- pypi์—์„œ ๊ฒ€์ƒ‰ํ•œ Requests ํŒจํ‚ค์ง€๋Š” ์‚ฌ์šฉ์ž์˜ ์ฝ”๋“œ์—์„œ 
- ์›น์‚ฌ์ดํŠธ๋กœ request๋ณด๋‚ด๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
- requests ํŒจํ‚ค์ง€์—์„œ getํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
- getํ•จ์ˆ˜๋Š” ์ด๋™ํ•œ ํ›„, website๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
from requests import get

URL Status Codes ํ™•์ธํ•˜๊ธฐ

- 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
"""

์š”์ฒญ๊ฐ’์˜ ์ƒํƒœ์ฝ”๋“œ ์‘๋‹ต์œผ๋กœ dictionary ๋งŒ๋“ค๊ธฐ

- 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'}
"""

sum() ํ•จ์ˆ˜

- 
profile
๐Ÿ“Œ FE ๊ณต๋ถ€ ์ •๋ฆฌ ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€