下面的脚本是from this site。它目前不工作,但我已经使它在我自己的计算机上工作(目前无法访问)。然而,我真正想要的是利用这个脚本返回一个元组(self.tomatometer, self.audience)
(看看函数def _process(self)
)。你知道吗
我要做的是向这个脚本传递一个电影标题列表(在for
循环中),并让它将self.tomatometer
和self.audience
变量返回给调用者。你知道吗
我成功地做到了这一点,但它似乎没有得到赞扬和令人费解:假设我把这个脚本称为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
我认为你根本不应该这样做。你知道吗
_process()
的前缀是_
,因为正如PyCharm警告的那样,它应该是一个私有类方法。这意味着它应该只在类本身中使用,而不是由您使用。你知道吗您正在用电影标题初始化
RottenTomatoesRating
类的实例,然后对该实例调用._process()
。当您调用RottenTomatoesRating
类RottenTomatoesRating(movie_title)
的构造函数时,它将执行该类的__init__()
方法,并将您的电影标题作为title
参数传入。__init__()
方法还调用self._process()
,从而将值分配给self.tomatometer
和self.audience
(如果可用)。然后可以直接访问这些值:相关问题 更多 >
编程相关推荐