
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
작성한 코드 및 에러 메세지
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')
#old_content > table > tbody > tr:nth-child(2)
#old_content > table > tbody > tr:nth-child(3)
#old_content > table > tbody > tr:nth-child(4)
#old_content > table > tbody > tr:nth-child(2) > td.title > div > a
#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(3) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(4) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(5) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(2) > td.point
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
if title is not None:
title = movie.select_one('td.title > div > a').text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(title, rank, star)
아래는 오류코드입니다.
Traceback (most recent call last):
File "C:/Users/enter/OneDrive/바탕 화면/sparta/pythonprac/3주차 뷰티풀수프 3회독.py", line 24, in <module>
if title is not None:
NameError: name 'title' is not defined
종료 코드 1(으)로 완료된 프로세스
이렇게 쓰면 왜 오류가 나는 것인가요?
