
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')
trs=soup.select('#old_content > table > tbody > tr')
for tr in trs:
rank=tr.select_one('td:nth-child(1) > img')
title=tr.select_one('td.title > div > a')
point=tr.select_one('td.point')
if title is not None:
print(rank['alt'],title.text,point.text)
이걸 실행하면 잘 출력되는데 왜
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')
trs=soup.select('#old_content > table > tbody > tr')
for tr in trs:
rank=tr.select_one('td:nth-child(1) > img')['alt']
title=tr.select_one('td.title > div > a')
point=tr.select_one('td.point')
if title is not None:
print(rank,title.text,point.text)
이걸 실행하면
Yeon@DESKTOP-NHJNF1L MINGW64 /c/Users/Rho/sparta
$ C:/Users/노정연/AppData/Local/Programs/Python/Python38/python.exe c:/Users/Rho/sparta/pythonprac/hello.py
Traceback (most recent call last):
File "c:/Users/Rho/sparta/pythonprac/hello.py", line 12, in <module>
rank=tr.select_one('td:nth-child(1) > img')['alt']
TypeError: 'NoneType' object is not subscriptable
뜨는데 왜 그런건가요..?(['alt']의 위치만 변경하였습니다)
