본문 바로가기

14. 웹스크래핑 연습

by Rudy 2021. 9. 2.
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://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')

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

for tr in trs:
    a_tag=tr.select_one('td.title > div > a')
    if a_tag is not None:
        rank=tr.select_one('td:nth-child(1) > img')['alt']
        title=a_tag.text
        star=tr.select_one('td.point')
        print(rank,title,star.text)

영화 페이지에서 영화 순위, 영화 제목, 별점을 차례대로 출력하는 코드 작성

영화 페이지의 개발자도구(검사)->copy selector를 잘 이용해서 코드에 적용하면 된다.

 

'' 카테고리의 다른 글

16. 웹스크래핑 연습  (0) 2021.09.04
15. pymongo로 DB조작하기  (0) 2021.09.03
13. 웹 크롤링  (0) 2021.09.01
11. 원페이지 쇼핑페이지에 환율 api 적용  (0) 2021.08.27
10. Ajax 연습  (0) 2021.08.26

댓글