logo
Loading...

Yahoo! 電影ans和sample中不懂的部分 - Cupoy

你好,我在做day014練習時遇到2個問題想請問老師1.day014的ans部分:for movie...

Yahoo! 電影ans和sample中不懂的部分

2020/06/28 10:40 上午
Python網路爬蟲討論版
佳鈴
觀看數:8
回答數:2
收藏數:1

你好,我在做day014練習時遇到2個問題想請問老師

1.day014的ans部分:

for movie_theater in html:

    print("----------------------------------------------------------------------")

    

    theater_name = movie_theater.find("li", attrs={'class':'adds'})

    print("電影院: ", theater_name.a.string)


    project_type = movie_theater.find_all("li", attrs={'class':'taps'})  # 定位放映類型

        

    time_table = movie_theater.find_all("li", attrs={'class':'time _c'})  # 定位時間表

        

    for i in range(len(project_type)):

        print("放映類型: ", project_type[i].span.get_text())  # 印出電影類型

        

        for p4 in time_table[i].find_all("input", attrs={'name':'schedule_list'}):

            print("%s" % (p4["value"]))  # 印出放映時間

想請問為什麼for i in range(len(project_type))要用len

2.day014的sample的部分:

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-7-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'

如果只印satisfaction可以印出來,但是抓不到滿意度,想請問是否有其他方式可以抓到滿意度的值呢?