【问题标题】:Is there a way to extract IMDb reviews using IMDbPY?有没有办法使用 IMDbPY 提取 IMDb 评论?
【发布时间】:2020-05-15 01:59:36
【问题描述】:

我不需要 Kaggle 中提供的数据集。我想使用 IMDbPY 或任何其他抓取方法从 IMDb 中提取电影评论。

https://imdbpy.github.io/

【问题讨论】:

  • 嘿,你有没有试过任何代码?如果是这样,请分享您拥有的东西,我们可以从中开始提供帮助。 (stackoverflow.com/help/how-to-ask)
  • 查看文档和源代码,该库似乎无法访问评论。虽然它可能隐藏在某个地方
  • @byxor 肯定有。但是有点隐蔽。但这只是为了确保我们不会从 imdb 下载超出需要的数据。

标签: python web-scraping imdb imdbpy


【解决方案1】:

是的,您可以使用 IMDbPY 提取评论。 Colab Notebook.

# to install the imdbpy library, just including it for noob-friendliness     
pip install imdbpy

这是您必须了解的有关 IMDbPY 的内容,它分别使用方法 get_movie、get_person 和 get_company 从 IMDB 检索各种对象的数据,例如电影、人物和公司。然而,事情是有很多信息要检索,检索所有内容可能不是最好的解决方案(因为它会消耗时间和带宽)。因此,数据被分组为称为“信息集”的小部分信息。

检索电影“黑客帝国 (1999)”的代码。
(注意:“0133093”是IMDb标题的ID,没有'tt',例如:https://www.imdb.com/title/tt0133093/

from imdb import IMDb
ia = IMDb()
theMatrix = ia.get_movie('0133093')

默认情况下,电影对象具有以下信息集'main'、'plot'、'synopsis',您可以使用.current_info进行检查。现在我们可以从这里看到,默认情况下,电影对象不会检索“评论”信息集。

theMatrix.current_info

#output:
['main', 'plot', 'synopsis']

如果您知道要检索哪些信息集,我们可以将可选参数“info=”传递给 get_movie 方法。在这种情况下,“评论”。

theMatrix = ia.get_movie('0133093',['reviews'])
theMatrix.current_info

#output:
['reviews']

theMatrix['reviews']

#output:
[{'author': 'ur0540275',
  'content': "The story of a reluctant Christ-like protagonist...",
  'date': '19 September 2000',
  'helpful': 0,
  'not_helpful': 0,
  'rating': 1,
  'title': ''},
 {'author': 'ur15794099',
  'content': '** May contain spoilers **There aren\'t many movies...',
  'date': '26 July 2014',
...
...

如果您已经检索了一个电影对象并希望包含更多信息集而不必再次检索整个电影对象,那么 update 方法可能会有所帮助。

theMatrix = ia.get_movie('0133093')
theMatrix.current_info

#output
['main', 'plot', 'synopsis']

ia.update(theMatrix,['reviews'])
theMatrix.current_info

#output
['main', 'plot', 'synopsis', 'reviews']

以上详述的两种方式不仅可以帮助您获得“评论”,还可以帮助您获得任何您想要检索的其他信息集。但是,您需要知道每个对象(电影、个人或公司)支持的可用信息集是什么。为此,您可以分别使用 ia.get_movie_infoset、ia.get_person_infoset 或 ia.get_company_infoset 方法。

sorted(ia.get_movie_infoset())

#output:
['airing',
 'akas',
 'alternate versions',
 'awards',
 'connections',
 'crazy credits',
 'critic reviews',
 'episodes',
 'external reviews',
 ...
 ...
 'release dates',
 'release info',
 'reviews',
 'sound clips',
 'soundtrack',
 'synopsis',
 'taglines',
 'technical',
 'trivia',
 'tv schedule',
 'video clips',
 'vote details']

有了所有这些理论,可以更好地学习和理解 imdbpy。这是获取电影评论的单线:)

ia.get_movie_reviews('0133093')

#output:
[{'author': 'ur0540275',
  'content': "The story of a reluctant Christ-like protagonist...",
  'date': '19 September 2000',
  'helpful': 0,
  'not_helpful': 0,
  'rating': 1,
  'title': ''},
 {'author': 'ur15794099',
  'content': '** May contain spoilers **There aren\'t many movies...',
  'date': '26 July 2014',
...
...

【讨论】:

    【解决方案2】:

    虽然从imdbpy docs 中并不明显。您始终可以通过检查变量的键来检查变量的属性。当您使用 imdbpy 抓取电影时,并非您要查找的所有信息都立即可用。在您的情况下,您希望获得评论。所以你必须添加它们。我们可以在信息集中看到,有三种不同类型的评论; “评论”、“外部评论”和“评论评论”。与这些关联的键尚未添加。下面的例子展示了它是如何完成的。

    from imdb import IMDb
    
    # create an instance of the IMDb class
    ia = IMDb()
    
    the_matrix = ia.get_movie('0133093')
    print(sorted(the_matrix.keys()))
    
    # show all information sets that can be fetched for a movie
    print(ia.get_movie_infoset()) #Information we can add. Keys will be added
    ia.update(the_matrix, ['external reviews'])
    ia.update(the_matrix, ['reviews'])
    ia.update(the_matrix, ['critic reviews'])
    # show which keys were added by the information set
    print(the_matrix.infoset2keys['external reviews']) #no external reviews, so no key is added
    print(the_matrix.infoset2keys['reviews']) # A lot of reviews. Adds key: 'reviews'
    print(the_matrix.infoset2keys['critic reviews']) #Adds the keys: 'metascore', and 'metacritic url'
    # print(the_matrix['reviews'])
    print(sorted(the_matrix.keys())) #Check out the new keys that we have added
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 2015-10-28
      • 2014-09-10
      • 2015-12-11
      • 2021-07-26
      • 1970-01-01
      相关资源
      最近更新 更多