import requests # requests 라이브러리 설치 필요
r = requests.get('http://spartacodingclub.shop/sparta_api/seoulair') #r=requests.get()여기에 api 주소를 써준다
rjson = r.json()
rows= rjson['RealtimeCityAir']['row'] #json 에서 보고자 하는 데이터들을 지정해준다
for row in rows:
gu_name = row['MSRSTE_NM']. #rows 안에 'MSRTE_NM'을 gu_name 으로 지정한다
gu_mise = row['IDEX_MVL']
gu_status = row['IDEX_NM']
print(gu_name,gu_mise, gu_status) #지정헌 미세먼지 구이름, 미세먼지 수치, 미세먼지 상태를 호출한다
#네이버 영화에서 requests 와 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=20221007',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
#old_content > table > tbody > tr:nth-child(4) > td.title > div > a
#영화 제목을 누르고 copy selector 했을 때 #old_content > table > tbody > tr 까지 반복하는것을 봤다
#크롤링하고자 하는 영화 정보들을 movies라고 지칭하고 tr 까지의 데이터를 전부 담는다
# for 문을 사용해서 찾고자 하는 세부 데이터들을 호출한다
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)
데이터를 잘 정리해서 쉽게 찾기위해 사용한다
DB 종류
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparta@cluster0.v0amks8.mongodb.net/?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
# 저장 - 예시
doc = {'name':'bobby','age':21}. #딕셔너리를 만들어주고 데이터를 넣어준다
db.users.insert_one(doc) # DB user에 doc 라는 딕셔너리를 저장한다
# 한 개 찾기 - 예시
user = db.users.find_one({'name':'bobby'}) #user라는 DB에서 이름이 bobby인 데이터를 하나 찾는다
# 여러개 찾기 - 예시 ( _id 값은 제외하고 출력)
all_users = list(db.users.find({},{'_id':False})) #전체 유저를 user DB에서 리스트 형식으로 찾아라 단 '_id'값은 제외해라
# 바꾸기 - 예시
db.users.update_one({'name':'bobby'},{'$set':{'age':19}}) #user 라는 DB에서 이름이 bobby 인 데이터를 찾고 age 를 19로 바꿔라
# 지우기 - 예시
db.users.delete_one({'name':'bobby'}) #user 라는 DB에서 이름이 bobby인 데이터를 삭제하라
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://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20221007',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1)
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
songs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for song in songs:
song = soup.select_one('td.info > a.title.ellipsis').text.strip()
rank = soup.select_one('td.number').text[:2].strip()
artist = soup.select_one('td.info > a.artist.ellipsis').text
print(rank,song, artist)
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://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20221007',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1)
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
songs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for song in songs:
title = song.select_one('td.info > a.title.ellipsis').text.strip()
rank = song.select_one('td.number').text[:2].strip()
artist = song.select_one('td.info > a.artist.ellipsis').text
print(rank,title, artist)