2022.10.20 작성
Python 리스트 추가 방법
a_list.append()
Python 함수
def sum(a,b)
return a+b
result = sum(1,2)
Python 조건문
def is_adult(age):
if age > 20:
print('성인입니다')
else:
print('청소년이에요')
is_adult(30)
Python 반복문
fruits = ['사과','배','배','감','수박','귤','딸기','사과','배','수박']
count = 0
for fruit in fruits:
if fruit == '사과':
count += 1
print(count)
Requests 이용해서 미세먼지 값 가져오기
import requests # requests
r = requests.get('http://spartacodingclub.shop/sparta_api/seoulair')
rjson = r.json()
rows = rjson['RealtimeCityAir']['row']
for row in rows:
gu_name = row['MSRSTE_NM']
gu_mise = row['IDEX_MVL']
print(gu_name,gu_mise)
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.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank,title,star)
pymongo 기본 코드
저장
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)
한 개 찾기
user = db.users.find_one({'name':'bobby'})
여러개 찾기 ( _id 값은 제외하고 출력)
all_users = list(db.users.find({},{'_id':False}))
바꾸기
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
지우기
db.users.delete_one({'name':'bobby'})
mongoDB에서 특정 데이터 가져오기
movie= db.movies.find_one({'title':'가버나움'})
star = movie['star']
all_movies = list(db.movies.find({'star':star},{'_id':False}))
for m in all_movies:
print(m['title'])