2017-05-28 64 views
1

我正在爲我的作業製作一個電影網站,但我已經收到了我還沒學過的代碼。如何使打印不可見(網頁創建)?

我正在使用3個文件,所以我可以利用我的學習類對象。

我遇到了其中一個問題。 (如下所示)。

我使用此代碼來處理所有我設置的電影的細節(參數)。 我已經設置了我的參數=None,因爲我列出的一些電影經過該過程的功能少於所需的參數。即在html文件中,它導致文本輸出'None'。

import webbrowser 

# Uses class method to organise movie contents. 
#INPUT (1)title, (2)description, (3)poster image url, (4)youtube trailer, (5)running time/duration (6)number of season, (7)number of episode, (8)tv station 
#all arguments uses string elements. 
class Movie(): 
    """This class provides a way to store movie and tv shows related information""" 
    def __init__(self, movie_title, movie_storyline, poster_image, trailer_youtube, duration_time=None, number_of_seasons=None, tv_station=None): 
     self.title = movie_title 
     self.storyline = movie_storyline 
     self.poster_image_url = poster_image 
     self.trailer_youtube_url = trailer_youtube 
#  blank = ' ' 
#  if duration_time == True: 
#   return blank 
#  else: 
     self.duration = duration_time 
     self.seasons = number_of_seasons 
     self.station = tv_station 

    def show_trailer(self): 
     """Opens Trailer in webbrowser""" 
     webbrowser.open(self.trailer_youtube_url) 

下面是它的屏幕截圖 - 我突出顯示了它在哪裏打印出文本'無'。 enter image description here

我該如何更改爲文字,而不是文字「無」。

html代碼通過python 這是在不同的文件中,注意。

import webbrowser 
import os 
import re 
print "" 
#ready html tags -> use following code 
#print("Content-Type: text/HTML") 

# Styles and scripting for the page 
main_page_head = ''' 
<head> 
    <meta charset="utf-8"> 
    <title>Fresh Tomatoes!</title> 

    <!-- Bootstrap 3 --> 
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css"> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script> 
    <style type="text/css" media="screen"> 
     body { 
      padding-top: 80px; 
      background-color: #eee; 
     } 
     #trailer .modal-dialog { 
      margin-top: 200px; 
      width: 640px; 
      height: 480px; 
     } 
     .hanging-close { 
      position: absolute; 
      top: -12px; 
      right: -12px; 
      z-index: 9001; 
     } 
     #trailer-video { 
      width: 100%; 
      height: 100%; 
     } 
     .movie-tile { 
      margin-bottom: 20px; 
      padding-top: 20px; 
     } 
     .movie-tile:hover { 
      background-color: #d1d1d1; 
      cursor: pointer; 
     } 
     .scale-media { 
      padding-bottom: 56.25%; 
      position: relative; 
     } 
     .scale-media iframe { 
      border: none; 
      height: 100%; 
      position: absolute; 
      width: 100%; 
      left: 0; 
      top: 0; 
      background-color: white; 
     } 
    </style> 
    <script type="text/javascript" charset="utf-8"> 
     // Pause the video when the modal is closed 
     $(document).on('click', '.hanging-close, .modal-backdrop, .modal', function (event) { 
      // Remove the src so the player itself gets removed, as this is the only 
      // reliable way to ensure the video stops playing in IE 
      $("#trailer-video-container").empty(); 
     }); 
     // Start playing the video whenever the trailer modal is opened 
     $(document).on('click', '.movie-tile', function (event) { 
      var trailerYouTubeId = $(this).attr('data-trailer-youtube-id') 
      var sourceUrl = 'http://www.youtube.com/embed/' + trailerYouTubeId + '?autoplay=1&html5=1'; 
      $("#trailer-video-container").empty().append($("<iframe></iframe>", { 
       'id': 'trailer-video', 
       'type': 'text-html', 
       'src': sourceUrl, 
       'frameborder': 0 
      })); 
     }); 
     // Animate in the movies when the page loads 
     $(document).ready(function() { 
      $('.movie-tile').hide().first().show("fast", function showNext() { 
      $(this).next("div").show("fast", showNext); 
      }); 
     }); 
    </script> 
</head> 
''' 

# The main page layout and title bar 
main_page_content = ''' 
<!DOCTYPE html> 
<html lang="en"> 
    <body> 
    <!-- Trailer Video Modal --> 
    <div class="modal" id="trailer"> 
     <div class="modal-dialog"> 
     <div class="modal-content"> 
      <a href="#" class="hanging-close" data-dismiss="modal" aria-hidden="true"> 
      <img src=""/> 
      </a> 
      <div class="scale-media" id="trailer-video-container"> 
      </div> 
     </div> 
     </div> 
    </div> 

    <!-- Main Page Content --> 
    <div class="container"> 
     <div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
     <div class="container"> 
      <div class="navbar-header"> 
      <a class="navbar-brand">Find your Favourite Movies</a> 
      </div> 
     </div> 
     </div> 
    </div> 
    <div class="container"> 
     {movie_tiles} 
    </div> 
    <footer style='text-align: center;'><em>A Udacity project</em></footer> 
    </body> 
</html> 
''' 

# A single movie entry html template 
movie_tile_content = ''' 
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="{trailer_youtube_id}" data-toggle="modal" data-target="#trailer"> 
    <img src="{poster_image_url}" width="220" height="342"> 
    <h2>{movie_title}</h2> 
    alt="" title="hover text"<p>{movie_storyline}<p> 
    <p>{duration_time} {number_of_seasons}</p> 
    <p>{tv_station}</p> 
</div> 
''' 

def create_movie_tiles_content(movies): 
    # The HTML content for this section of the page 
    content = '' 
    for movie in movies: 
     # Extract the youtube ID from the url 
     youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer_youtube_url) 
     youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer_youtube_url) 
     trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None 

     # Append the tile for the movie with its content filled in 
     content += movie_tile_content.format(
      movie_title=movie.title, 
      poster_image_url=movie.poster_image_url, 
      trailer_youtube_id=trailer_youtube_id, 
      movie_storyline=movie.storyline, 
      duration_time=movie.duration, 
      number_of_seasons=movie.seasons, 
      tv_station=movie.station 
     ) 
    return content 

def open_movies_page(movies): 
    # Create or overwrite the output file 
    output_file = open('fresh_tomatoes.html', 'w') 

    # Replace the placeholder for the movie tiles with the actual dynamically generated content 
    rendered_content = main_page_content.format(movie_tiles=create_movie_tiles_content(movies)) 

    # Output the file 
    output_file.write(main_page_head + rendered_content) 
    output_file.close() 

    # open the output file in the browser 
    url = os.path.abspath(output_file.name) 
    webbrowser.open('file://' + url, new=2) # open in a new tab, if possible 
+0

請包括您的HTML文件的相關部分。 – Soviut

+0

當您未指定給定參數時,您正在通過「無」作爲默認值。只需使用類似''''的東西來刪除默認的'None'邏輯,或者更好的是,在沒有指定參數的情況下運行一個條件,當*指定*時只輸出內容。 –

回答

2

一個快速的解決方案是通過一個空字符串''代替None。在Python中,無論如何,空字符串的計算結果都是「falsey」,所以兩者是相似的。唯一的區別是空字符串將打印爲空白。

def __init__(self, movie_title, movie_storyline, poster_image, trailer_youtube, duration_time='', number_of_seasons='', tv_station=''): 

另一種選擇是爲您的HTML模板添加一些邏輯。你沒有在問題中包含你的HTML,所以考慮這個僞代碼。

{% if number_of_seasons %} 
    <div class="seasons">{{ number_of_seasons }}</div> 
{% endif %} 
+0

該代碼給我的項目和一些JavaScript和其他語言在那裏我還沒有學到。但我確實知道大量的html和python。 – MonocleBoo