logo
Loading...

出現AttributeError: 'NoneType' object has no attribute 'span'的訊息?要如何解決 - Cupoy

max_page = 5for page_number in range(1, max_page+1...

出現AttributeError: 'NoneType' object has no attribute 'span'的訊息?要如何解決

2020/06/26 04:10 AM
Python網路爬蟲論壇
佳鈴
觀看數:0
回答數:2
收藏數:0

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'



[ ]: