Python

  • requests 패키지 써보기
    import requests # requests 라이브러리 설치 필요
    
    r = requests.get('http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99')
    rjson = r.json()
    
    print(rjson)
  • ajax 보다 짧고 간결하게 해당 값을 불러올 수 있다.👍🏻

javascript와 python 비교

import requests # requests 라이브러리 설치 필요

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

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

for gu in gus:
    gu_name = gu['MSRSTE_NM']
    gu_mise = gu['IDEX_MVL']
    if gu_mise < 70:
        print(gu_name, gu_mise)
$.ajax({
    type: "GET",
    url: "http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99",
    data: {},
    success: function (response) {
        let rows = response['RealtimeCityAir']['row']
        for (let i = 0; i < rows.length; i++) {
            let gu_name = rows[i]['MSRSTE_NM']
            let gu_mise = rows[i]['IDEX_MVL']

크롤링

  • 크롤링 해보기(크롤링을 위해서 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')
    
    trs = soup.select('#old_content > table > tbody > tr')
    
    for tr in trs:
        a_tag = tr.select_one('td.title > div > a')
        if a_tag is not None:
            title = a_tag.text
            rank = tr.select_one('td:nth-child(1) > img')['alt']
            star = tr.select_one('td.point').text
            print(rank, title, star)​
  • 검사->copy selector

mongodb/robo3T

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

# insert / find / update / delete

# 저장 - 예시
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'})

아~~~~~~주 중요함!! 여러번 연습해봐야겠따.... 특히 2번 퀴즈⭐️

+ Recent posts