
안녕하세요 선생님
3-8 크롤링 강의의 최종코드는 아래와 같습니다.
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.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
그중에서 이 부분을 보시면
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
아래와 같이 코드를 써도 똑같이 출력이 되는데.
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
rank = movie.select_one('td:nth-child(1) > img')
star = movie.select_one('td.point')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
a태그를 정의를 해주면 정상적으로 코드가 출력이 됩니다. 하지만 a태그를 정의해주지 않으면 출력이 되지 않습니다.
그런데 star랑 rank는 a태그 안에 있지 않고 star는 td태그안에 있고, rank는 img태그 안에 있습니다.
title만 a태그 안에 있습니다.
a태그를 정의해주지 않고 아래 코드와 같이 rank또는 star만 정의해주면 코드가 출력되지 않습니다.
rank 와 star 를 정의해주지 않아도, a태그만 정의 해줘도 출력이 되는 이유가 무엇인가요?
a태그를 정의해주지 않고 rank또는 star만 아래와 같이 코드를 작성하면 출력이 안됩니다.
a 만 정의 => 코드출력 0
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
rank = movie.select_one('td:nth-child(1) > img')
star = movie.select_one('td.point')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
rank만 정의 => 코드출력 x
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
# a = movie.select_one('td.title > div > a')
rank = movie.select_one('td:nth-child(1) > img')
# star = movie.select_one('td.point')
if rank is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
.star만 정의 => 코드출력 x
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
# a = movie.select_one('td.title > div > a')
# rank = movie.select_one('td:nth-child(1) > img')
star = movie.select_one('td.point')
if star is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
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.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# title = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.title > div > a')
# print(title) <a href="/movie/bi/mi/basic.naver?code=186114" title="밥정">밥정</a>
# print(title['href']) /movie/bi/mi/basic.naver?code=186114
# print(title.text) 밥정
#old_content > table > tbody > tr:nth-child(2) > td.title > div > a
#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
#old_content > table > tbody > tr:nth-child(4) > td.title > div > a
# # for movie in movies:
# # a = movie.select_one('td.title > div > a')
# # if a is not None:
# print(a.text)
#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img
# for movie in movies:
# rank = movie.select_one('td:nth-child(1) > img')
# if rank is not None:
# print(rank['alt'])
#old_content > table > tbody > tr:nth-child(5) > td.point
#
# for movie in movies:
# star = movie.select_one('td.point')
# if star is not None:
# print(star.text)
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
# a = movie.select_one('td.title > div > a')
# rank = movie.select_one('td:nth-child(1) > img')
star = movie.select_one('td.point')
if star is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
항상 친절하게 답변해주시는 선생님들께 감사드립니다!
