2012-12-06 71 views
1
import imdb  
ia = imdb.IMDb() 
avatar = ia.get_movie("0120667") 
ia.update(avatar, 'business') 
print avatar['business'] 

返回整個列表的總數,以及每個國家的放映。但是,我如何才能獲得篩選信息?而且只有1個國家。在這個例子中我想要得到的信息是(美國)(2005年7月10日)(3602個屏幕)IMDBpy獲取屏幕信息

回答

2
import imdb 
import re 

ia = imdb.IMDb() 
avatar = ia.get_movie("0120667") 
ia.update(avatar, 'business') 
opening_weekends = avatar['business']['opening weekend'] 

def parseDate(date): 
    result = {} 

    if re.match(".*\d{4}$", date): 
     result['year'] = date[-4:] 

    m = re.match(".*(?P<month>January|February|March|April|May|June|July|" 
       "August|September|October|November|December).*", date, re.I) 
    if m: 
     result['month'] = m.group('month').lower() 

     # try to grab date too then 
     daymatch = re.match("^(?P<day>\d{1,2}).*", date) 

     if daymatch: 
      result['day'] = daymatch.group('day')  
    return result 

def parseBudget(amount): 
    """ 
    assumptions: 
    - currency is always before the number 
    - no fractions 
    """ 

    # find index first number 
    for i in range(len(amount)): 
     if amount[i] in "": 
      amount_idx = i 
      break 

    currency = amount[:amount_idx].strip() 
    amount = re.sub("\D", "", amount[amount_idx:]) 

    return amount, currency 

def parseWeekendGross(gross_text): 
    g = gross_text.split(' (') 
    if not len(g) == 4: 
     return "" 
    amount, currency = parseBudget(g[0]) 
    country = g[1].lstrip('(').rstrip(')') 
    date = parseDate(g[2].lstrip('(').rstrip(')')) 
    day, month, year = date['day'], date['month'], date['year'] 
    screens = re.sub("\D", "", g[3]) 
    if not screens: 
     screens = "''" 

    return amount, currency, country, day, month, year, screens 

for entry in opening_weekends: 
    amount, currency, country, day, month, year, screens = parseWeekendGross(entry) 
    if country == "USA": 
     print("Country: %s" % country) 
     print("Date: %s %s %s" % (day, month, year)) 
     print("Screens: %s" % screens) 
     break 

上面的代碼給我下面的結果:

Country: USA 
Date: 10 july 2005 
Screens: 3602 

功能解析數據從該項目複製而來:pyIRDG