크롤링 (Beautiful Soup 활용 기술)

import requests
from bs4 import BeautifulSoup

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

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=D&ymd=20200403&hh=23&rtm=N&pg=1',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

#print(soup)

 #number path : #body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
 #song path : #body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
 #singer path : #body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis

trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')

for tr in trs:
    numbers = tr.select_one('td.number')
    songs = tr.select_one('td.info > a.title.ellipsis')
    singers = tr.select_one('td.info > a.artist.ellipsis')

    if numbers is not None and songs is not None and singers is not None :
        number = numbers.text[0]
        song = songs.text.strip()
        singer = singers.text.strip()

        print(number, song, singer)

DB - 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'})
profile
왕초보

0개의 댓글

Powered by GraphCDN, the GraphQL CDN