a = 3 # 3을 a에 넣는다
b = a # a를 b에 넣는다
a = a + 1 # a+1을 다시 a에 넣는다
num1 = ab # (ab의 값을 num1이라는 변수에 넣는다)
num2 = 99 # (99의 값을 num2이라는 변수에 넣는다)
--------------------------------------------------
name = 'bob' # (변수에는 문자열이 들어갈 수도 있고,)
num = 12 # (숫자가 들어갈 수도 있고,)
is_number = True # True 또는 False -> "Boolean"형이 들어갈 수도 있습니다.
# a_list[0]의 값은? 1
# a_list[1]의 값은? [2,3]
# a_list[1][0]의 값은? 2
# a_dict의 값은? {'name':'bob','age':21, 'height':178}
# a_dict['name']의 값은? 'bob'
# a_dict['age']의 값은? 21
# a_dict['height']의 값은? 178
people = [{'name':'bob','age':20},{'name':'carry','age':38}]
# people[0]['name']의 값은? 'bob'
# people[1]['name']의 값은? 'carry'
person = {'name':'john','age':7}
people.append(person)
# people의 값은? [{'name':'bob','age':20},
{'name':'carry','age':38},{'name':'john','age':7}]
# people[2]['name']의 값은? 'john'
--------------------------------------------------
# (수학문제에서)
f(x) = 2*x+3
y = f(2)
y의 값은? 7
# (참고: 자바스크립트에서는)
function f(x) {
return 2*x+3
# (파이썬에서)
def f(x):
return 2*x+3
y = f(2)
y의 값은? 7
def sum_all(a,b,c):
return a+b+c
def mul(a,b):
return a*b
--------------------------------------------------
def oddeven(num): # oddeven이라는 이름의 함수를 정의한다. num을 변수로 받는다.
if num % 2 == 0: # (num을 2로 나눈 나머지가 0이면)
return True # (True (참)을 반환한다.)
else: # (아니면,)
return False # (False (거짓)을 반환한다.)
result = oddeven(20)
def is_adult(age):
if age > 20:
print('성인입니다') # (조건이 참이면 성인입니다를 출력)
else:
print('청소년이에요') # (조건이 거짓이면 청소년이에요를 출력)
is_adult(30)
--------------------------------------------------
* 파이썬에서의 반복문은, 리스트의 요소들을 하나씩 꺼내쓰는 형태입니다.
fruits = ['사과','배','감','귤']
for fruit in fruits:
print(fruit)
# (사과, 배, 감, 귤 하나씩 꺼내어 찍힙니다.)
--------------------------------------------------
import requests
from bs4 import BeautifulSoup
# (타겟 URL을 읽어서 HTML를 받아오고,)
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.naver?sel=pnt&date=20210829',headers=headers)
# (HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦)
# (soup이라는 변수에 "파싱 용이해진 html"이 담긴 상태가 됨 # 이제 코딩을 통해 필요한 부분을 추출하면 된다.)
soup = BeautifulSoup(data.text, 'html.parser')import requests
from bs4 import BeautifulSoup
# (URL을 읽어서 HTML를 받아오고,)
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.naver?sel=pnt&date=20210829',headers=headers)
# HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦
soup = BeautifulSoup(data.text, 'html.parser')
# select를 이용해서, tr들을 불러오기
movies = soup.select('#old_content > table > tbody > tr')
# (movies (tr들) 의 반복문을 돌리기)
for movie in movies:
# (movie 안에 a 가 있으면,)
a_tag = movie.select_one('td.title > div > a')
if a_tag is not None:
# (a의 text를 찍어본다.)
print (a_tag.text)
soup.select('태그명')
soup.select('.클래스명')
soup.select('#아이디명')
soup.select('상위태그명 > 하위태그명 > 하위태그명')
soup.select('상위태그명.클래스명 > 하위태그명.클래스명')
# (태그와 속성값으로 찾는 방법)
soup.select('태그명[속성="값"]')
# (한 개만 가져오고 싶은 경우)
soup.select_one('위와 동일')--------------------------------------------------
'users'라는 collection에 {'name':'bobby','age':21}를 넣습니다.
db.users.insert_one({'name':'bobby','age':21}) db.users.insert_one({'name':'kay','age':27}) db.users.insert_one({'name':'john','age':30})# 모든 데이터 뽑아보기
all_users = list(db.users.find({},{'_id':False}))print(all_users[0]) # (0번째 결과값을 보기)
print(all_users[0]['name']) # (0번째 결과값의 'name'을 보기)
for user in all_users: # (반복문을 돌며 모든 결과값을 보기)
print(user)
# 오타가 많으니 이 줄을 복사해서 씁시다!
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})user = db.users.find_one({'name':'bobby'})
print(user)
user = db.users.find_one({'name':'bobby'})
print(user)
--------------------------------------------------
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient client = MongoClient('mongodb+srv://test:sparta@cluster0.55vah.mongodb.net/Cluster0?retryWrites=true&w=majority') db = client.dbsparta
# URL을 읽어서 HTML를 받아오고,
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.naver?sel=pnt&date=20210829',headers=headers)# (HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦)
soup = BeautifulSoup(data.text, 'html.parser')# (select를 이용해서, tr들을 불러오기)
movies = soup.select('#old_content > table > tbody > tr')# (movies (tr들) 의 반복문을 돌리기)
for movie in movies:
# (movie 안에 a 가 있으면,)
a_tag = movie.select_one('td.title > div > a')
if a_tag is not None:
rank = movie.select_one('td:nth-child(1) > img')['alt']
# (img 태그의 alt 속성값을 가져오기)
title = a_tag.text
# (a 태그 사이의 텍스트를 가져오기)
star = movie.select_one('td.point').text
# (td 태그 사이의 텍스트를 가져오기)
print(rank,title,star)
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.55vah.mongodb.net/Cluster0?retryWrites=true&w=majority')
db = client.dbsparta
# (URL을 읽어서 HTML를 받아오고,)
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.naver?sel=pnt&date=20210829',headers=headers)
# (HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦)
soup = BeautifulSoup(data.text, 'html.parser')# (select를 이용해서, tr들을 불러오기)
# (movies (tr들) 의 반복문을 돌리기)
for movie in movies:
# (movie 안에 a 가 있으면,)
a_tag = movie.select_one('td.title > div > a')
if a_tag is not None:
rank = movie.select_one('td:nth-child(1) > img')['alt']
# (img 태그의 alt 속성값을 가져오기)
title = a_tag.text
# (a 태그 사이의 텍스트를 가져오기)
star = movie.select_one('td.point').text
# (td 태그 사이의 텍스트를 가져오기)
doc = {
'rank': rank,
'title': title,
'star': star
}
db.movies.insert_one(doc)
--------------------------------------------------
느낀점 : 아직 배워야할게 많다. 2주차에 비해서는 좀 더 수월하게 할 수 있었고, 튜터님의 해설에 전부는 아니더라도 부분적으로 맞추기도해서 어떤부분부터 잘 못되었는지 좀 더 집중해서 수강했던 것 같다. 4주차, 5주차는 지금까지의 과정에서 꽃을 피우는 것이라고 하셨는데 어려움을 느낄것이기에 걱정도 앞서지만 우선 1강을 완강을 목표로 해야겠다.