๋ณ์ (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) #์ฒญ์๋
์ด์์
๋ฐ๋ณต๋ฌธ
fruits = ['์ฌ๊ณผ','๋ฐฐ','๋ฐฐ','๊ฐ','์๋ฐ','๊ทค','๋ธ๊ธฐ','์ฌ๊ณผ','๋ฐฐ','์๋ฐ']
count = 0
for fruit in fruits:
if fruit == '์ฌ๊ณผ':
count += 1
print(count) #2
File - Settings - Projects:phythonprac > Python interpreter > (+) > ์ค์นํ๊ณ ์ํ๋ ํจํค์ง๋ช ๊ฒ์ ํ ์ค์น
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
ํฌ๋กค๋ง : ๊ฒ์์์ง์ด ์ฌ์ดํธ๋ฅผ ํผ๊ฐ๋ ํ์ ( ์ฝ๋์์ ์์ฒญํ๊ธฐ / ์ํ๋ ์ ๋ณด๋ฅผ ์ ์์๋ด๊ธฐ)
[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 ๋ถ๋ถ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋จ
์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์
ํ/์ด์ ์๊น์๊ฐ ์ ํด์ง ์์ ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ฒ๊ณผ ์ ์ฌ
๋์ ๋๋ฆฌ์ ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ DB, ๋ฐ์ดํฐ ํ๋ํ๋๋ง๋ค ๊ฐ์ ๊ฐ์ ๊ฐ์ง ํ์X
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'})
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)
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)
#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)
์ ๋ชฉ๊ณผ ๊ฐ์๋ฅผ ๊ฐ์ ธ์ฌ ๋ ๋ถํ์ํ ๊ณต๋ฐฑ์ด ๋ง์์ .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]