Python 如何使用以下类脚本返回值(元组)?

Python 如何使用以下类脚本返回值(元组)?,python,class,beautifulsoup,urllib,Python,Class,Beautifulsoup,Urllib,下面的脚本已拍摄。它目前不工作,但我已经在自己的计算机上工作(目前无法访问)。然而,我真正想要的是利用这个脚本返回一个元组(self.tomotometer,self.audience)(查看函数def\u process(self)) 我要做的是向脚本传递一个电影标题列表(在for循环中),并让它将self.tometer和self.viewer变量返回给调用者 我成功地做到了这一点,但它似乎不值得推荐且令人费解:假设我将此脚本称为convrt.py,这就是我所做的: import convr

下面的脚本已拍摄。它目前不工作,但我已经在自己的计算机上工作(目前无法访问)。然而,我真正想要的是利用这个脚本返回一个元组
(self.tomotometer,self.audience)
(查看函数
def\u process(self)

我要做的是向脚本传递一个电影标题列表(在
for
循环中),并让它将
self.tometer
self.viewer
变量返回给调用者

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

import convrt
# this is what I'm doing, it's working, but seems weird.
convrt.RottenTomatoesRating("Movie Title Here")._process()
PyCharm警告我正在访问类的私有方法。我知道Python中没有任何私有内容,这就是所谓的“名称混乱”,但我仍然认为这可能不是使用此脚本返回元组的最佳方式

原文:

#!/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

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

\u process()
\ucode>作为前缀,因为正如PyCharm警告您的那样,它应该是一个私有类方法。这意味着它应该只在类本身中使用,而不是由您使用

您正在初始化带有电影标题的
RottoMatoEsrating
类的实例,然后在该实例上调用
\u process()
。当您调用
rottomotosrating
类的构造函数--
rottomotosrating(movie\u title)
--它执行类的
\u init\u()
方法,并将您的movie title作为
title
参数传入。
\uuuu init\uuuuu()
方法还调用
self.\u process()
,从而为
self.tometer
self.viewer
中的每一个赋值。然后可以直接访问这些值:

import convrt

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

这就是所谓的“名称混乱”-不,这是两个前导下划线,所以您必须执行
ClassName.\u ClassName.\u方法\u name
。如果你的问题是关于软件开发过程中所做的设计决策,那么你必须问问是谁写的。谢谢。不,这不是我的问题。如果这是您认为可以改进的工作代码,您考虑过吗?经过我的修改后,它正在工作,请参阅
convrt.rottentomotosrating(“此处的电影标题”)。\u process()
,我已更改此方法以返回元组。然而,我想知道这种做法是否“错误”/“不和谐”。我来看看代码回顾,谢谢。谢谢。我把这个贴在代码评论上,那里的答案和你的答案真的为我完成了图片!