# requests 라이브러리 설치 필요
import requests
# 예시 url
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)
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://geuna:Fortuna1229!@cluster0.ctqzg.mongodb.net/?retryWrites=true&w=majority',
tlsCAFile=ca)
db = client.dbsparta
# 저장 - 예시
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에 저장되지 못하고 pymongo.errors.ServerSelectionTimeoutError: [SSL: CERTIFICATE_VERIFY_FAILED]...
에러가 발생했다.
certifi
패키지 설치import cerfiti
ca = certifi.where()
# 두번째 tlsCAFile=ca 중요!!
client = MongoClient('url', tlsCAFile=ca)
...
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=20220601', 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
# RANK
# #body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
# ARTIST
# #body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
musicList = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
# print(musicList)
for music in musicList:
rank = music.select_one('td.number').text[0:2].strip()
title = music.select_one('td.info > a.title.ellipsis').text.strip()
artist = music.select_one('td.info > a.artist.ellipsis').text
print(rank, '|', title, '|', artist)