Webページ-3の作成

2219 ワード

スクロール(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'})