본문 바로가기
웹 프로그래밍(풀스택-->python)

37. Quiz_웹스크래핑 결과 이용하기

by 백엔드개발자0107 2021. 4. 24.

 

웹 스크래핑을 하기전에 웹 크롤링으로 매트릭스 평점을 가져오는 복습을 해보자

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://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=pnt&date=20200303', headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select("#old_content > table > tbody > tr")

for tr in trs:
    point_tag = tr.select_one("td.point")
    title_tag = tr.select_one("td.title > div > a")
    if point_tag is not None and title_tag.text == "매트릭스":
        print(point_tag.text)


# old_content > table > tbody > tr:nth-child(20) > td.point
# old_content > table > tbody > tr:nth-child(27)
# old_content > table > tbody > tr:nth-child(20) > td.point
# old_content > table > tbody > tr:nth-child(20) > td.title > div > a

 

이런식으로 웹 크롤링을 할수가 있다.

 

자 인제 웹 스크래핑(ifud-mongodb)를 해보자

 

 

 

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

target_movie = db.movies.find_one({"title": "매트릭스"}, {"_id": False})

print(target_movie["star"])

 

이런식으로 웹 스크래핑이 가능하다.

 

이번에는 매트릭스 평점과 같은 영화 제목들을 아래와 같이 가져와보자

 

 

 

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

target_movie = db.movies.find_one({"title": "매트릭스"}, {"_id": False})
target_star = target_movie["star"]
star_lists = list(db.movies.find({"star": target_star}, {"_id": False}))
for star in star_lists:
    print(star["title"])

 

마지막으로 매트릭스의 평점을 0으로 만들어보자

 

 

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

db.movies.update_one({"title": "매트릭스"}, {"$set": {"star": 0}})

 

 

이렇게 하면 된다.!

'웹 프로그래밍(풀스택-->python)' 카테고리의 다른 글

39. 앞으로 계속 배울것.  (0) 2021.04.26
38. 끝, 숙제를 해보자  (0) 2021.04.25
36. 웹스크래핑 결과 저장하기  (0) 2021.04.24
35. pymongo로 DB조작하기  (0) 2021.04.23
34. DB 개괄  (0) 2021.04.23