【发布时间】:2015-11-18 16:05:38
【问题描述】:
下面的脚本取自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
【问题讨论】:
-
"这就是所谓的 "name mangling"" - 不,这是 两个 前导下划线,所以你必须这样做 @ 987654331@。如果您的问题是关于在该软件开发过程中做出的设计决策,您必须询问编写它的人。
-
谢谢。不,这不是我的问题。
-
如果这是您认为可以改进的工作代码,您考虑过Code Review吗?
-
我修改后它正在工作,请参阅
convrt.RottenTomatoesRating("Movie Title Here")._process(),我已更改此方法以返回一个元组。但是,我想知道这种做事方式是否“错误”/unpythonic。我会看看 Code Review,谢谢。
标签: python class beautifulsoup urllib