[์คํ๋ฅดํ ์ฝ๋ฉ ํด๋ฝ]Python
@3-1. ๋ก๋ฉ ํ ๋ฐ๋ก ์คํ ์ฝ๋
$(document).ready(function(){
listing();
});
function listing() {
console.log('ํ๋ฉด ๋ก๋ฉ ํ ์ ์คํ๋์์ต๋๋ค');
}
@3-2. ํ์ด์ฌ (๋ณ์, ์๋ฃํ, ํจ์, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ)
๋ณ์ : # ์ซ์์, ๋ฌธ์์ด์ ๊ฐ์ด ์ธ๋ ex) first_name = 'jonghun' , num = (2) ๊ดํธ์ฐ๊ธฐ
๋ฆฌ์คํธ : #์์ ์ถ๊ฐ a_list.append('XX')
๋์ ๋๋ฆฌ : a_dict = {'name' : 'bob','age' : 27} print(a_dict['age']
ํจ์ : def sum(๋ค๋ฅธ ์ฉ์ด ๊ฐ๋ฅ)(num1, num2):
return num1+num2
result = num(1,2)
์กฐ๊ฑด๋ฌธ : age = 25
if age > 25:
print('์ฑ์ธ์ ๋๋ค')
else:
print('์ฒญ์๋ ์ ๋๋ค')
๋ฐ๋ณต๋ฌธ : fruits = ['์ฌ๊ณผ','๋ฐฐ','๋ฐฐ','๊ฐ','์๋ฐ','๊ทค','๋ธ๊ธฐ','์ฌ๊ณผ','๋ฐฐ','์๋ฐ']
for ff in fruits:
print(ff)
people = [{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27}]
for person in people:
if person['age'] < 20:
print(person)
>> john, smith
------------------------------------------------------
@3-3. ํจํค์ง
requests
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]['NO2'])
@3-3. ํฌ๋กค๋ง
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')
# ์ฝ๋ฉ ์์
title = soup.select_one('copy > copy select' ํ๊ธฐ)
print(title.text) > ๊ทธ๋ฆฐ๋ถ๋ง ๊ฐ์ ธ์ด, ํ๊ทธ์ ์์ฑ๋ง ๊ฐ์ ธ์ฌ๋ print(title['href'])
for tr in trs:
a_tag = tr.select_one('td.title > div > a')
if a_tag is not None:
rank = tr.select_one('td:nth-child(1) > img')['alt']
title = a_tag.text
star = tr.select_one('td.point').text
print(rank, title, star)
@3-4. DB๋ชฝ๊ณ (๋ฐ์ดํฐ๋ฅผ ์ ๊ฐ๋ค ์ฐ๋ ค๊ณ )
# ์ค์น ํ์ธ : ์ฃผ์์ฐฝ localhost:27017 ๊ฒ์ํ ~~~~~ HTTP on the native driver port.
# ๊ธฐ๋ณธ ์ฝ๋
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta
# ์ ์ฅ - ์์
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)
# ํ ๊ฐ ์ฐพ๊ธฐ - ์์
user = db.users.find_one({'name':'bobby'})
# ์ฌ๋ฌ๊ฐ ์ฐพ๊ธฐ - ์์ ( _id ๊ฐ์ ์ ์ธํ๊ณ ์ถ๋ ฅ)
same_ages = list(db.users.find({'age':21},{'_id':False}))
# ๋ฐ๊พธ๊ธฐ - ์์
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
# ์ง์ฐ๊ธฐ - ์์
db.users.delete_one({'name':'bobby'})
DB ์ซ์ >> ๋ฌธ์์ด๋ก ๋ฐ๊พธ๊ธฐ {'age':'19'}
delet_many : ๋ชจ๋์ญ์
#๋ผ์ด๋ธ๋ฌ๋ฆฌ : requests, bs4, pymongo,