出現AttributeError: 'NoneType' object has no attribute 'span'的訊息?要如何解決
max_page = 5
for page_number in range(1, max_page+1, 1):
url = 'https://movies.yahoo.com.tw/movie_intheaters.html'
payload = {'page':str(page_number)}
resp = requests.get(url, params=payload)
resp.encoding = 'utf-8'
soup = BeautifulSoup(resp.text, 'html.parser')
# 把電影介紹擷取出來
movie_list = soup.find("ul", attrs={"class":"release_list"}).find_all("li")
for p in movie_list:
print("----------------------------------------------------------------------")
# 電影名稱
movie_name = p.find("div", attrs={"class":"release_movie_name"})
print("電影名稱:", movie_name.a.string)
# 定位電影評價資訊欄位
level_box = movie_name.find("dl", attrs={"class":"levelbox"})
# 期待度
expectation = level_box.find("div", attrs={"class":"leveltext"})
print("期待度:", expectation.span.string)
# 滿意度
satisfaction = level_box.find("div", attrs={"class":"leveltext starwithnum"})
print("滿意度:", satisfaction.span["data-num"]) # 滿意度可以從"data-num"這個屬性擷取
# 簡介
movie_info = p.find("div", attrs={"class":"release_text"})
print(movie_info.span.string)
AttributeError Traceback (most recent call last) <ipython-input-11-7c4cf0c40cc0> in <module> 24 # 滿意度 25 satisfaction = level_box.find("div", attrs={"class":"leveltext starwithnum"}) ---> 26 print("滿意度:", satisfaction.span["data-num"]) # 滿意度可以從"data-num"這個屬性擷取 27 28 # 簡介 AttributeError: 'NoneType' object has no attribute 'span'
[ ]:
回答列表
-
2020/06/27 上午 02:24張維元 (WeiYuan)贊同數:0不贊同數:0留言數:1
嗨,佳鈴
你可以試著先把「satisfaction」印出來看看嗎?
如果這個回答對你有幫助請主動點選「有幫助」的按鈕,也可以追蹤我的GITHUB帳號。若還有問題的話,也歡迎繼續再追問或者把你理解的部分整理上來,我都會提供你 Review 和 Feedback 😃😃😃
-
2020/07/01 上午 01:20張維元 (WeiYuan)贊同數:0不贊同數:0留言數:0
嗨,原因是 Yahoo! 電影新改版之後滿意度已經沒有 starwithnum class 了,因此用這個是無法抓到資料的。建議可以試著模仿 期待度 的方式自己找找看。
如果這個回答對你有幫助請主動點選「有幫助」的按鈕,也可以追蹤我的GITHUB帳號。若還有問題的話,也歡迎繼續再追問或者把你理解的部分整理上來,我都會提供你 Review 和 Feedback 😃😃😃