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
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)
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)
user = db.users.find_one({'name':'bobby'})
複数の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'})
Reference
この問題について(Webページ-3の作成), 我々は、より多くの情報をここで見つけました https://velog.io/@emanneung/Web-Page-작성-3テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol