如何使用 scrapy 和 beautifulsoup 从 imdb 中提取预算、总分、元分数?

How to extract budget, gross, metascore from imdb using scrapy and beautifulsoup?

我正盯着下面的 url:

http://www.imdb.com/chart/top

HTML 文件的结构似乎很混乱:

” 元分数:“

我正在尝试使用这样的格式:

movie['metascore'] = self.get_text(soup.find('h4', attrs={'&nbsp':'Metascore'}))

我会试一试,因为听起来您对抓取还不熟悉。听起来您实际上正在尝试做的是从 IMDB 上的 250 个电影页面中的每一个页面获取预算、总分和元分数。提及 Scrapy 是正确的,因为您必须从您提供的初始 URL 抓取这些页面。 Scrapy 有一些 excellent documentation,所以如果你想使用它,我强烈建议你先从那里开始。

但是,如果您只需要抓取这 250 页,最好只使用 Beautiful Soup 来完成整个工作。只需执行 soup.findAll("td", {"class":"titleColumn"}),提取链接,然后执行一个循环,让 Beautiful Soup 一次打开每个页面。如果您不确定该怎么做,BS 有 excellent documentation.

从那里开始,只需在每次迭代期间抓取所需的相关数据即可。例如,每部电影的元分数都在 class star-box-details<div> 内。为此做一个 .find 然后你必须做一些正则表达式来提取你想要的确切部分 (regular-expressions.info has a great tutorial on regex and if you really get into regex, you'll probably end up sinking hours into RexEgg).

我不会编写整个代码,因为您会通过尝试解决问题所带来的反复试验学到很多东西,但希望这能让您走上正轨。但是,请注意 IMDB forbids scraping, but for small projects I'm sure no one will care. But if you want to get serious, the "Does IMDB provide an API?" post has some excellent resources for how to do it via various third-party APIs (and some even directly from IMDB). In your case, the best might be to simply download the data as text files directly from IMDB. Click on any of the FTP links. The files you'll probably want are business.list.gz and ratings.list.gz. As for the metascore on each movie page, that rating actually comes from Metacritic,因此您需要去那里提取该数据。

祝你好运!