๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

3-8_Quiz_์›น์Šคํฌ๋ž˜ํ•‘(ํฌ๋กค๋ง) ์—ฐ์Šต 1 ๋ณธ๋ฌธ

[WEB & AI] (feat. ์ทจ์ค€)/์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ_์›น๊ฐœ๋ฐœ์ข…ํ•ฉ๋ฐ˜

3-8_Quiz_์›น์Šคํฌ๋ž˜ํ•‘(ํฌ๋กค๋ง) ์—ฐ์Šต 1

๋ถ€์‹œ๋งค๋‚˜_HA 2023. 10. 13. 07:08
728x90
๋ฐ˜์‘ํ˜•

โ… . ์ด๋ฒˆ ํšŒ์ฐจ ํ•™์Šต ๋ชฉํ‘œ (goal)


1. ๋ฐฐ์šฐ๊ณ ์ž ํ•˜๋Š” ๊ฒƒ

  • ์ˆœ์œ„ ๋Š” ์˜ค๋ฅธ์ชฝ ๊ฒ€์‚ฌ ํด๋ฆญ -> ์ด๊ฑฐ ๊ฐ€์ ธ์˜ค๊ธฐ

  • ํ‰์  ์€ ์˜ค๋ฅธ์ชฝ ๊ฒ€์‚ฌํด๋ฆญ -> ์ด๊ฑฐ ๊ฐ€์ ธ์˜ค๊ธฐ



โ…ก. ํ•ด๋ณด๊ธฐ


1.

  • ํ•ด๋‹น๊ฐ’์€ ์ด๋ ‡๊ฒŒ ์ ‘๊ทผํ•˜๋ผ๊ณ  ํžŒํŠธ๋ฅผ ์ค€๋‹ค.

  • ์ž˜ ๋ชฐ๋ž๋˜ ๊ฒƒ

    • tr ๊นŒ์ง€ ๊ฐ€์ ธ์™”์œผ๋‹ˆ๊นŒ, ๋’ค์—๊ฑธ ๋ถ™์ด๋ฉด๋œ๋‹ค. ํ˜น์€ html ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐ€์ ธ์˜ค๋Š”๊ฑฐ โญโญโญโญโญ
    • .text ๋ฅผ ์œ„๋กœ ๊ฐ€์ ธ์™€๋„ ๋œ๋‹ค๋Š” ๊ฒƒ โญโญโญโญโญ

02-02 2์ฐจ ๋ณต์Šต

  • ์šฐ์„  ๋Œ์•„๊ฐ€๋Š” ์ฝ”๋“œ
    import requests
    from bs4 import BeautifulSoup
    

์‹ค์ œ ์‚ฌ๋žŒ์ธ ๊ฒƒ ์ฒ˜๋Ÿผ ํ•ด์ฃผ๋ ค๊ณ  (๋งˆ์น˜, ๋ธŒ๋ผ์šฐ์ €์—์„œ call ์„ ๋‚ ๋ฆฌ๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ํ•ด์ฃผ๋ ค๊ณ , ์‚ฌ๋žŒ์ธ ๊ฒƒ ์ฒ˜๋Ÿผ ํ•ด์ฃผ๋ ค๊ณ )

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}

ํ•ด๋‹น url ์—์„œ html ์„ ๋ฐ›์•„์˜จ๋‹ค. (โœ… url ๋ณ€๊ฒฝํ•ด์„œ ์‚ฌ์šฉ)

data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)

html์—์„œ ์›ํ•˜๋Š” ๋ถ€๋ถ„์„ ์†Ž์•„๋‚ด๊ธฐ ์œ„ํ•ด ์šฐ์„  '์ „์ฒด๋ฅผ ๋‹ด์•„์˜จ๋‹ค.'

soup = BeautifulSoup(data.text, 'html.parser')

์ด์ œ ๋ถ€ํ„ฐ ๋‚˜์˜ค๋Š” ๋ฌธ๋ฒ•์€ beaurifulsoup ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž์ฒด์˜ ๋ฌธ๋ฒ•(syntax)

๋‹ด์•„ ์˜จ ๊ฒƒ ์ค‘ copy selector๋ฅผ select_one ์— ๋„ฃ์–ด์„œ ์†Ž์•„๋‚ด๊ธฐ.

title = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.title > div > a')

# .text, ['href'] ๋ฌธ๋ฒ•์œผ๋กœ ์›ํ•˜๋Š” ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ

print(title)

print(title.text)

print(title['href'])

๊ณตํ†ต์˜ copy selector ๋ฅผ ์“ฐ๋ฉด select ํ•จ์ˆ˜์— ๋ณต์ˆ˜๊ฐœ์˜ copy selector ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ.

ex) '๋ฐฅ์ •' ๊ณผ '๊ทธ๋ฆฐ ๋ถ' copy selector ๋ฅผ ์ฐ์–ด๋ด„

#old_content > table > tbody > tr:nth-child(2) > td.title > div > a
#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
# ๊ณตํ†ต๋˜๋Š” ๋ถ€๋ถ„์„ select ํ•จ์ˆ˜์— ๋„ฃ๊ธฐ 

movies = soup.select('#old_content > table > tbody > tr')

for movie in movies:
a = movie.select_one('td.title > div > a')
if a != None: # if a is not None ({๋™์ผ})
print(a.text)

[๋ชฉํ‘œ] 1) rank 2) title 3) star ๊ฐ€์ ธ์˜ค๊ธฐ

rank = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img')['alt']

title = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.title > div > a').text

star = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.point').text

print(rank,title,star)

movies = soup.select('#old_content > table > tbody > tr')
print(movies)

for movie in movies:

a = movie.select_one('td:nth-child(1) > img')
if a != None:
    rank = a['alt']

    title = movie.select_one('td.title > div > a').text # title ๊นŒ์ง€ ์˜ค๋Š” html ๋ฉ์–ด๋ฆฌ ๋‚ด์—์„œ๋„ None ์ด ์กด์žฌํ•˜์ง€ ์•Š์•˜์„๊นŒ? ๊ทธ๋Ÿฐ๋ฐ ์™œ ์ด ๋ถ€๋ถ„์—์„œ๋Š” ๊ฒฐ์ธก๊ฐ’์ด ์•ˆ ๋‚˜์˜ค๋Š”๊ฑฐ์ง€? 

    star = movie.select_one('td.point').text
    print(rank, title, star)

```

  • ์ค‘์š”ํ•œ ๊ฒƒ
    • ๊ตฌ์ฒด์ ์œผ๋กœ ์›ํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ, html ๊ตฌ์กฐ ์•„๋ž˜์—์„œ ์–ด๋–ป๊ฒŒ ์ง€์นญ ํ•  ๊ฒƒ ์ธ๊ฐ€.
  • select ๋กœ '๊ณตํ†ต๋ถ€๋ถ„' ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ, ์›์น˜ ์•Š๊ฒŒ None ์„ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋ถ„๋ช…ํžˆ ์ƒ๊น€ โญโญโญ

  • ๋”ฐ๋ผ์„œ, select ๋กœ ๊ณตํ†ต ๋ถ€๋ถ„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒฝ์šฐ์—” ๊ฒฐ์ธก๊ฐ’ ์œ ์˜!

  • ์˜๋ฌธ์ 

    • ๊ฒฐ์ธก๊ฐ’ ๊ฒ€์‚ฌ๋ฅผ ํ•  ๋•Œ, ์™œ ํ•œ ๋ฒˆ๋งŒ ํ•˜๋ฉด ๋˜๋Š”๊ฑฐ์ง€โ“โ“โ“โ“โ“
    • title ์„ ๋จผ์ € ๋นผ์ฃผ๊ณ , rank ๋ฅผ ๋Œ๋ ค๋„ ๋ฌธ์ œ๊ฐ€ ์—†๊ฒŒ ๋œ๋‹ค. ์ด๊ฒŒ ์™œ ๊ทธ๋Ÿฐ๊ฑฐ์ง€...
    • ๋ฐ˜๋Œ€๋กœ ์–ด์ œ๋Š” title ์„ ๋จผ์ € ํ–ˆ๊ณ , ์˜ค๋Š˜์€ rank ๋ฅผ ๋จผ์ € ํ–ˆ๋Š”๋ฐ...
  • ์ค‘์š”ํ•œ ์ 
    • select_one ์— ๊ธฐ๋ฐ˜์—์„œ , ๊ทธ ๋ฃจํŠธ๋ฅผ ๋ณต๋ถ™ํ•ด์„œ select ํ•œ๋‹ค.


2. ์šฐ์„  ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ

#jquery_๋ณ€์ˆ˜ํ™œ์šฉ๊ฝ‚์•„์ฃผ๊ธฐ_์˜ˆ์‹œ์ฝ”๋“œ
#jquery_append_์ถ”๊ฐ€๋กœ๋ถ™์ด๊ธฐ_์˜ˆ์‹œ์ฝ”๋“œ



3. design critic



โ…ข. ํ•ด๋ณด๊ธฐ


1. ํ•ด๋ด



2. ์šฐ์„  ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ

#jquery_๋ณ€์ˆ˜ํ™œ์šฉ๊ฝ‚์•„์ฃผ๊ธฐ_์˜ˆ์‹œ์ฝ”๋“œ
#jquery_append_์ถ”๊ฐ€๋กœ๋ถ™์ด๊ธฐ_์˜ˆ์‹œ์ฝ”๋“œ



3. design critic




๐Ÿ’ญ What i learned


-

728x90
๋ฐ˜์‘ํ˜•
Comments