3์ฃผ์ฐจ WIL๐Ÿฑโ€๐Ÿ‘คPython,ํฌ๋กค๋ง,mongoDB

Winteringยท2022๋…„ 4์›” 9์ผ
0

๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
3/17

Python ๐ŸŽ€

  • ๋ณ€์ˆ˜ (int๋‚˜ let ๊ฐ™์€ ๊ฒŒ ์—†์Œ)

    a = 3      
    b = 2       
    
    num1 = a*b 
    num2 = 99 
  • ์ž๋ฃŒํ˜•

    #์ˆซ์ž, ๋ฌธ์ž
    name = 'abc'		#๋ฌธ์ž์—ด์€ ''
    num = 12
    is_number = True	#boolean๊ฐ’ ๊ฐ€๋Šฅ
    
    #๋ฆฌ์ŠคํŠธํ˜•(Javascript์™€ ๋™์ผ)
    a_list = []
    a_list.append(1)		#๋ฆฌ์ŠคํŠธ '๊ฐ’' ์ถ”๊ฐ€
    a_list.append([2,3])	#๋ฆฌ์ŠคํŠธ์— '๋ฆฌ์ŠคํŠธ' ์ถ”๊ฐ€
    
    print(a_list)			# 1,[2,3]
    print(a_list[0])		# 1
    print(a_list[1])		# [2,3]
    print(a_list[1][0])		# 2
    
    #๋”•์…”๋„ˆ๋ฆฌ
    a_dict = {}
    a_dict = {'name':'bob','age':21}
    a_dict['height'] = 178				#ํ‚ค๊ฐ’์ง€์ •ํ•ด์„œ ๊ฐ’์ž…๋ ฅ๋งŒํ•ด๋„ ์ž๋™์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€
    
    print(a_dict['name'])		#'bob'
    print(a_dict['age'])		# 21
    print(a_dict['height'])		#178
    
    #๋”•์…”๋„ˆ๋ฆฌ+๋ฆฌ์ŠคํŠธ
    people = [{'name':'bob','age':20},{'name':'carry','age':38}]
    
    print(people[0]['name'])	#'bob'
    print(people[1]['name'])	#'carry'
    
    person = {'name':'john','age':7}
    people.append(person)
    
    print(people)				
    #[{'name':'bob','age':20},{'name':'carry','age':38},{'name':'john','age':7}]
    print(people[2]['name'])	#'john'
  • ํ•จ์ˆ˜

    ```def f(X):
        return 2*x+3
    
    y=f(2)
    print(y)		#7
  • ์กฐ๊ฑด๋ฌธ

    def is_adult(age):
    	if age > 20:
    		print('์„ฑ์ธ์ž…๋‹ˆ๋‹ค')   
    	else:
    		print('์ฒญ์†Œ๋…„์ด์—์š”')
    
    is_adult(30)		#์„ฑ์ธ์ž…๋‹ˆ๋‹ค
    is_adult(15)		#์ฒญ์†Œ๋…„์ด์—์š”
  • ๋ฐ˜๋ณต๋ฌธ

    • python์˜ ๋ฐ˜๋ณต๋ฌธ์€ ๋ฆฌ์ŠคํŠธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ
    fruits = ['์‚ฌ๊ณผ','๋ฐฐ','๋ฐฐ','๊ฐ','์ˆ˜๋ฐ•','๊ทค','๋”ธ๊ธฐ','์‚ฌ๊ณผ','๋ฐฐ','์ˆ˜๋ฐ•']
    
    count = 0
    for fruit in fruits:
    	if fruit == '์‚ฌ๊ณผ':
    		count += 1
    
    print(count)			#2

Python ํŒจํ‚ค์ง€ ์„ค์น˜ ๐ŸŽ€

File - Settings - Projects:phythonprac > Python interpreter > (+) > ์„ค์น˜ํ•˜๊ณ ์žํ•˜๋Š” ํŒจํ‚ค์ง€๋ช… ๊ฒ€์ƒ‰ ํ›„ ์„ค์น˜

  • requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ, ๋ฏธ์„ธ๋จผ์ง€ Open API ๋ฐ›์•„์˜ค๊ธฐ
import requests # requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ํ•„์š”

r = requests.get('http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99')
rjson = r.json()


# print(rjson['RealtimeCityAir']['row'][0]['MSRSTE_NM'])

gus = rjson['RealtimeCityAir']['row']

for gu in gus:
    gu_name = gu['MSRSTE_NM']
    gu_mise = gu['IDEX_MVL']
    if gu_mise > 90:
        print(gu_name, gu_mise)

์„œ๋Œ€๋ฌธ๊ตฌ 93.0
๋™๋Œ€๋ฌธ๊ตฌ 91.0
๊ฐ•์„œ๊ตฌ 91.0
๊ธˆ์ฒœ๊ตฌ 91.0

์›น์Šคํฌ๋ž˜ํ•‘(ํฌ๋กค๋ง)๐ŸŽ€

ํฌ๋กค๋ง : ๊ฒ€์ƒ‰์—”์ง„์ด ์‚ฌ์ดํŠธ๋ฅผ ํผ๊ฐ€๋Š” ํ–‰์œ„ ( ์ฝ”๋“œ์—์„œ ์š”์ฒญํ•˜๊ธฐ / ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ž˜ ์†Ž์•„๋‚ด๊ธฐ)

  1. [bs4] ํŒจํ‚ค์ง€ ์„ค์น˜ (ํฌ๋กค๋ง ๊ธฐ๋ณธ์„ธํŒ…)

    import requests
    from bs4 import BeautifulSoup
    
    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'}
    data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=pnt&date=20200303', headers=headers)
    
    soup = BeautifulSoup(data.text, 'html.parser')
  • select_one

    • slelect_one('') ์—์„œ ''์•ˆ์— copy selector์˜ ๊ฒฐ๊ณผ๋ฌผ ๋ถ™์—ฌ๋„ฃ๊ธฐ

      title = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.title > div > a') 	
      
      print(title)
      # <a href="/movie/bi/mi/basic.naver?code=171539" title="๊ทธ๋ฆฐ ๋ถ">๊ทธ๋ฆฐ ๋ถ</a>
      
      print(title.text)		#ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•
      # ๊ทธ๋ฆฐ ๋ถ
      
      print(title['href'])	#ํƒœ๊ทธ์˜ ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•
      # /movie/bi/mi/basic.naver?code=171539
  • select

    : ์œ„์™€ ๊ฐ™์€ ๋ชฉ๋ก๋“ค์„ ์ญˆ์šฑ copy selector ํ•ด๋ณด๋ฉด
    
    #old_content > table > tbody > tr:nth-child(1)
    
    #old_content > table > tbody > tr:nth-child(2)
    
    #old_content > table > tbody > tr:nth-child(3)
    
    ์ฒ˜๋Ÿผ ๋’ค์˜ **nth_child(n)**๋งŒ ๋‹ค๋ฅด๊ณ , **#old_content > table > tbody > tr**๋Š” ๊ณตํ†ต์ธ ๊ฑธ ์•Œ ์ˆ˜ ์žˆ์Œ
    
    ```python
    trs = soup.select('#old_content > table > tbody > tr')
    
    for tr in trs:
        print(tr)
    ```

  • select + select_one

    trs = soup.select('#old_content > table > tbody > tr')
    
    #old_content > table > tbody > tr:nth-child(2) > td.title > div > a 
    #(๋žญํ‚น๋“ค ์ค‘ '๊ทธ๋ฆฐ๋ถ(์ด๋ฆ„๋ถ€๋ถ„)'์˜ selector)
    #select๋กœ tr๊นŒ์ง€์˜ ์ •๋ณด๋ฅผ ์ฐพ์•˜๊ณ , ๊ทธ ๋’ค์˜ td.title > div > a ๋ถ€๋ถ„์„ select_one์œผ๋กœ ์ง€์ •
    
    for tr in trs:
       a_tag = tr.select_one('td.title > div > a')
       print(a_tag)


์ค‘๊ฐ„์ค‘๊ฐ„ ๋œจ๋Š” none์€ ํ˜•์‹์— ๋”ฐ๋ฅธ ์˜ค๋ฅ˜์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ.
์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํƒœ๊ทธ์—์„œ ''๊ทธ๋ฆฐ๋ถ'','๊ฐ€๋ฒ„๋‹ˆ์›€'์ฒ˜๋Ÿผ ํ…์ŠคํŠธ๋งŒ ๋ฝ‘์•„๋‚ด๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์•„๊นŒ์ฒ˜๋Ÿผ pirnt(a_tag.text) ๋ฅผ ํ•˜๋ฉด none ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋‚จ

์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์€

Database ๐ŸŽ€

- RDBMS(SQL)

ํ–‰/์—ด์˜ ์ƒ๊น€์ƒˆ๊ฐ€ ์ •ํ•ด์ง„ ์—‘์…€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌ

  • ๋ฐ์ดํ„ฐ๊ฐ€ 50๋งŒ๊ฐœ ์ ์žฌ๋  ๊ฒฝ์šฐ, ์ค‘๊ฐ„์— ์—ด์„ ๋”ํ•˜๋Š” ํ–‰์œ„๋Š” ์–ด๋ ค์›€
  • ์ •ํ˜•ํ™” ๋˜์–ด ์žˆ๋Š” ๋งŒํผ, ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ/ ๋ถ„์„์— ์šฉ์ด
  • ex) MySQL, Oracle

- No-SQL

๋”•์…”๋„ˆ๋ฆฌ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” DB, ๋ฐ์ดํ„ฐ ํ•˜๋‚˜ํ•˜๋‚˜๋งˆ๋‹ค ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์งˆ ํ•„์š”X

  • ex) MongoDB

pymongo๋กœ DB ์กฐ์ž‘๐ŸŽ€

File > Settings > Python interpreter > (+) pymongo

  • ํŒŒ์ด๋ชฝ๊ณ  ๊ธฐ๋ณธ์ฝ”๋“œ

    from pymongo import MongoClient
    client = MongoClient('localhost', 27017)
    db = client.dbsparta
    
    # ์ฝ”๋”ฉ ์‹œ์ž‘
    
    #insert
    doc = {'name':'bobby','age':21}
    db.users.insert_one(doc)
    
    #find
    same_ages = list(db.users.find({'age':21},{'_id':False}))
    
    #find_one
    user = db.users.find_one({'name':'bobby'})
    
    #update_one
    db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
    
    #delete_one
    db.users.delete_one({'name':'bobby'})

pythonQuiz(์ง€๋‹ˆ๋ฎค์ง ํฌ๋กค๋ง)๐ŸŽ€

import requests
from bs4 import BeautifulSoup

from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta

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'}
data = requests.get('https://www.genie.co.kr/chart/top200?ditc=D&ymd=20200403&hh=23&rtm=N&pg=1', headers=headers)

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

print(soup)

#title
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
#singer
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
#rank
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number

trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')

for tr in trs:
    t_tag = tr.select_one('td.info> a.title.ellipsis').text.strip()
    s_tag = tr.select_one('td.info> a.artist.ellipsis').text.strip()
    r_tag = tr.select_one('td.number').text

    print(r_tag,t_tag,s_tag)
  1. header์˜ ๋งํฌ๋ฅผ genie๋ฎค์ง url๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธฐ
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'}
data = requests.get('https://www.genie.co.kr/chart/top200?ditc=D&ymd=20200403&hh=23&rtm=N&pg=1', headers=headers)
  1. ํฌ๋กฌ > ๊ฒ€์‚ฌ > copy selector๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๋ถ€๋ถ„์˜ selector ๊ฐ€์ ธ์˜ค๊ธฐ
#title
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
#singer
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
#rank
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
  1. ์ฝ”๋“œ ์ž‘์„ฑ
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')

for tr in trs:
    t_tag = tr.select_one('td.info> a.title.ellipsis').text.strip()
    s_tag = tr.select_one('td.info> a.artist.ellipsis').text.strip()
    r_tag = tr.select_one('td.number').text

    print(r_tag,t_tag,s_tag)
  • ์ œ๋ชฉ๊ณผ ๊ฐ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ์ด ๋งŽ์•„์„œ .strip() ์„ ๋ถ™์—ฌ ๊ณต๋ฐฑ์ œ๊ฑฐ

  • soup.select์—์„œ ๊ณตํ†ต๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜จ๋‹ค๊ณ ,
    ('#body-content > div.newest-list > div > table > tbody > tr:nth-child(1)') ์ด๋ ‡๊ฒŒ ๊ฐ€์ ธ์˜ค๋ฉด ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๊ฐ™์ด ๊ฐ€์ ธ์™€์ง tr๊นŒ์ง€๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

  • rank์˜ ๊ฒฝ์šฐ ์œ„ ์ฝ”๋“œ๋กœ๋Š”

์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ ๋จ.
'td.number'์˜ ์ •๋ณด๊ฐ€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ!

ํ•„์š”ํ•œ ๊ฑด ์ฒ˜์Œ์˜ "1"

td.number์— ๋‹ด๊ฒจ์žˆ๋Š” text ์ค‘ "1 " ์˜ ํ˜•ํƒœ์—์„œ (")(1)()(") 0๋ฒˆ์งธ~3๋ฒˆ์งธ ์ค‘ 2๋ฒˆ์งธ๊นŒ์ง€ํ•„์š”

    r_tag = tr.select_one('td.number').text[0:2]

.text[0:?] 0~?๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€ ์ง€์ •

strip() ๋นˆ์นธ ์—†์• ์ค€๋‹ค. lstrip rstripํ•˜๋ฉด ์™ผ์ชฝ์—ฌ๋ฐฑ ์˜ค๋ฅธ์ชฝ์—ฌ๋ฐฑ ์—†์•ค๋‹ค.

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