如何使用以下 Class 脚本 return 一个值(元组)?

How to return a value (tuple) using the following Class script?

下面的脚本取自from this site。它目前不工作,但我已经让它在我自己的电脑上工作(目前无法访问)。然而,我真正想要的是利用这个脚本来return一个元组(self.tomatometer, self.audience)(看函数def _process(self))。

我想要做的是向这个脚本传递一个电影标题列表(在 for 循环中)并让它 return self.tomatometerself.audience 变量给来电者。

我设法做到了这一点,但它似乎不被推荐且令人费解:假设我将此脚本称为 convrt.py,这就是我所做的:

import convrt
# this is what I'm doing, it's working, but seems weird.
convrt.RottenTomatoesRating("Movie Title Here")._process()

PyCharm 警告我正在访问 class 的私有方法。我知道 in Python 中实际上没有任何私有内容,这就是所谓的 "name mangling",但我仍然认为这可能不是最好的方法有元组 return 使用此脚本编辑过吗?

原剧本:

#!/usr/bin/env python
# RottenTomatoesRating
# Laszlo Szathmary, 2011 (jabba.laci@gmail.com)

from BeautifulSoup import BeautifulSoup
import sys
import re
import urllib
import urlparse

class MyOpener(urllib.FancyURLopener):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15'

class RottenTomatoesRating:
    # title of the movie
    title = None
    # RT URL of the movie
    url = None
    # RT tomatometer rating of the movie
    tomatometer = None
    # RT audience rating of the movie
    audience = None
    # Did we find a result?
    found = False

    # for fetching webpages
    myopener = MyOpener()
    # Should we search and take the first hit?
    search = True

    # constant
    BASE_URL = 'http://www.rottentomatoes.com'
    SEARCH_URL = '%s/search/full_search.php?search=' % BASE_URL

    def __init__(self, title, search=True):
        self.title = title
        self.search = search
        self._process()

    def _search_movie(self):
        movie_url = ""

        url = self.SEARCH_URL + self.title
        page = self.myopener.open(url)
        result = re.search(r'(/m/.*)', page.geturl())
        if result:
            # if we are redirected
            movie_url = result.group(1)
        else:
            # if we get a search list
            soup = BeautifulSoup(page.read())
            ul = soup.find('ul', {'id' : 'movie_results_ul'})
            if ul:
                div = ul.find('div', {'class' : 'media_block_content'})
                if div:
                    movie_url = div.find('a', href=True)['href']

        return urlparse.urljoin( self.BASE_URL, movie_url )

    def _process(self):
        if not self.search:
            movie = '_'.join(self.title.split())

            url = "%s/m/%s" % (self.BASE_URL, movie)
            soup = BeautifulSoup(self.myopener.open(url).read())
            if soup.find('title').contents[0] == "Page Not Found":
                url = self._search_movie()
        else:
            url = self._search_movie()

        try:
            self.url = url
            soup = BeautifulSoup( self.myopener.open(url).read() )
            self.title = soup.find('meta', {'property' : 'og:title'})['content']
            if self.title: self.found = True

            self.tomatometer = soup.find('span', {'id' : 'all-critics-meter'}).contents[0]
            self.audience = soup.find('span', {'class' : 'meter popcorn numeric '}).contents[0]

            if self.tomatometer.isdigit():
                self.tomatometer += "%"
            if self.audience.isdigit():
                self.audience += "%"
        except:
            pass

if __name__ == "__main__":
    if len(sys.argv) == 1:
        print "Usage: %s 'Movie title'" % (sys.argv[0])
    else:
        rt = RottenTomatoesRating(sys.argv[1])
        if rt.found:
            print rt.url
            print rt.title
            print rt.tomatometer
            print rt.audience

我认为你根本不应该这样做。

_process()_ 为前缀,因为它应该是私有 class 方法,正如 PyCharm 警告您的那样。这意味着它只能在 class 内部使用,而不能由您使用。

您正在使用电影标题初始化 RottenTomatoesRating class 的实例,然后在该实例上调用 ._process()。当您调用 RottenTomatoesRating class 的构造函数时 - RottenTomatoesRating(movie_title) - 它会执行 class 的 __init__() 方法,并将您的电影标题作为title 参数。 __init__() 方法还调用 self._process(),从而为每个 self.tomatometerself.audience 分配值(如果可用)。然后您可以直接访问这些值:

import convrt

ratings = convrt.RottenTomatoesRating("Movie Title Here")
tomatometer = ratings.tomatometer
audience = ratings.audience