【问题标题】:How to scrape data from imdb business page?如何从 imdb 业务页面抓取数据?
【发布时间】:2015-02-24 06:15:04
【问题描述】:

我正在制作一个需要来自 imdb 业务页面的数据的项目。我正在使用 python。数据存储在两个标签之间,如下所示:

预算

$220,000,000(估计)

我想要数字金额,但到目前为止还没有成功。有什么建议。

【问题讨论】:

  • 曾经考虑过正则表达式吗?
  • 请告诉我们你尝试了什么。
  • 在 urllib 模块中使用正则表达式。
  • 我已经尝试过正则表达式,但是我需要的数据的结构方式使得我无法弄清楚使用什么方法来获取它。我是 python 新手,所以我不知道所有的 d方法。

标签: python imdb


【解决方案1】:

看看Beautiful Soup,它是一个有用的抓取库。如果您查看源代码,“预算”在 h4 元素内,而值在 DOM 中的下一个。这可能不是最好的例子,但它适用于您的情况:

import urllib
from bs4 import BeautifulSoup


page = urllib.urlopen('http://www.imdb.com/title/tt0118715/?ref_=fn_al_nm_1a')
soup = BeautifulSoup(page.read())
for h4 in soup.find_all('h4'):
    if "Budget:" in h4:
        print h4.next_sibling.strip()

# $15,000,000

【讨论】:

  • 您好,感谢您的解决方案......它非常适合这个链接,实际上我已经从这个页面提取了数据:)......我真正想要的是从业务页面获取数据.. ..这是链接:imdb.com/title/tt0118715/business?ref_=tt_dt_bus。你的和我尝试的任何代码都不适用于这个链接......我对python比较陌生,所以我不太了解:)但无论如何谢谢
  • 只是您感兴趣的预算值吗?来自业务页面的源代码在 h5 元素而不是 h4 中显示“预算”。因此,如果您更改它并从“预算:”中弹出冒号,它应该会为您提供价值。
【解决方案2】:

好吧,您要求使用 python,并且要求提供抓取解决方案。

但是不需要 python,也不需要抓取任何东西,因为预算数字可以在 http://www.imdb.com/interfaces 的 business.list 文本文件中找到

【讨论】:

    【解决方案3】:

    这是一大堆代码(您可以在此处找到您的要求)。
    下面的 Python 脚本将为您提供
    1) 来自 IMDb 的顶级票房电影列表
    2) 以及每个电影的演员列表 .

    from lxml.html import parse
    
    def imdb_bo(no_of_movies=5):
        bo_url = 'http://www.imdb.com/chart/'
        bo_page = parse(bo_url).getroot()
        bo_table = bo_page.cssselect('table.chart')
        bo_total = len(bo_table[0][2])
    
        if no_of_movies <= bo_total:
            count = no_of_movies
        else:
            count = bo_total
    
        movies = {}
    
        for i in range(0, count):
            mo = {}
            mo['url'] = 'http://www.imdb.com'+bo_page.cssselect('td.titleColumn')[i][0].get('href')
            mo['title'] = bo_page.cssselect('td.titleColumn')[i][0].text_content().strip()
            mo['year'] = bo_page.cssselect('td.titleColumn')[i][1].text_content().strip(" ()")
            mo['weekend'] = bo_page.cssselect('td.ratingColumn')[i*2].text_content().strip()
            mo['gross'] = bo_page.cssselect('td.ratingColumn')[(i*2)+1][0].text_content().strip()
            mo['weeks'] = bo_page.cssselect('td.weeksColumn')[i].text_content().strip()
    
            m_page = parse(mo['url']).getroot()
            m_casttable = m_page.cssselect('table.cast_list')
    
            flag = 0
            mo['cast'] = []
            for cast in m_casttable[0]:
                if flag == 0:
                    flag = 1
                else:
                    m_starname = cast[1][0][0].text_content().strip()
                    mo['cast'].append(m_starname)
    
            movies[i] = mo
    
        return movies
    
    
    if __name__ == '__main__':
    
        no_of_movies = raw_input("Enter no. of Box office movies to display:")
        bo_movies = imdb_bo(int(no_of_movies))
    
        for k,v in bo_movies.iteritems():
            print '#'+str(k+1)+'  '+v['title']+' ('+v['year']+')'
            print 'URL: '+v['url']
            print 'Weekend: '+v['weekend']
            print 'Gross: '+v['gross']
            print 'Weeks: '+v['weeks']
            print 'Cast: '+', '.join(v['cast'])
            print '\n'
    


    输出(在终端中运行):

    parag@parag-innovate:~/python$ python imdb_bo_scraper.py 
    Enter no. of Box office movies to display:3
    #1  Cinderella (2015)
    URL: http://www.imdb.com/title/tt1661199?ref_=cht_bo_1
    Weekend: $67.88M
    Gross: $67.88M
    Weeks: 1
    Cast: Cate Blanchett, Lily James, Richard Madden, Helena Bonham Carter, Nonso Anozie, Stellan Skarsgård, Sophie McShera, Holliday Grainger, Derek Jacobi, Ben Chaplin, Hayley Atwell, Rob Brydon, Jana Perez, Alex Macqueen, Tom Edden
    
    
    #2  Run All Night (2015)
    URL: http://www.imdb.com/title/tt2199571?ref_=cht_bo_2
    Weekend: $11.01M
    Gross: $11.01M
    Weeks: 1
    Cast: Liam Neeson, Ed Harris, Joel Kinnaman, Boyd Holbrook, Bruce McGill, Genesis Rodriguez, Vincent D'Onofrio, Lois Smith, Common, Beau Knapp, Patricia Kalember, Daniel Stewart Sherman, James Martinez, Radivoje Bukvic, Tony Naumovski
    
    
    #3  Kingsman: The Secret Service (2014)
    URL: http://www.imdb.com/title/tt2802144?ref_=cht_bo_3
    Weekend: $6.21M
    Gross: $107.39M
    Weeks: 5
    Cast: Adrian Quinton, Colin Firth, Mark Strong, Jonno Davies, Jack Davenport, Alex Nikolov, Samantha Womack, Mark Hamill, Velibor Topic, Sofia Boutella, Samuel L. Jackson, Michael Caine, Taron Egerton, Geoff Bell, Jordan Long
    

    【讨论】:

      【解决方案4】:

      试试IMDbPYits documentation。要安装,只需pip install imdbpy

      from imdb import IMDb
      ia = IMDb()
      movie = ia.search_movie('The Untouchables')[0]
      ia.update(movie)
      
      #Lots of info for the movie from IMDB
      movie.keys()
      

      虽然我不确定在哪里可以找到具体的预算信息

      【讨论】:

        猜你喜欢
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多