imdb前250部电影刮板-如何从2000年及以后获得结果?



我是Python(和一般的编码)的新手,所以我的知识都是基本的/有限的(我基本上只是从各种谷歌搜索中复制代码)。

我设法想出一个工作代码到目前为止:

import scrapy
class imdb_project(scrapy.Spider):
name = 'imdb'
start_urls = ['https://www.imdb.com/chart/top']
def parse(self, response):
for i in response.css('.titleColumn a'):
print(i.css('::text').get())

这段代码运行正常。我能够在https://www.imdb.com/chart/top上刮取所有250部电影标题

现在,我只想从同一页中抓取2000年及以后上映的电影标题。

你可以在这里看到年份显示在电影标题的后面。这应该是很容易做到的,但对于我的生活,我找不到一个类似的例子在谷歌上,甚至在这里Stack Overflow让我开始解决这个问题。

我想应该是一些简单的东西,比如:

if movie_year >= 2000:
then run the 'for' loop above

…但我不知道如何在Python中编写上述代码。任何帮助(如果可能的话,请不要使用regex或xpath)?

IMDb禁止抓取,但如果理论上允许你从他们的网站上抓取,这是它的样子:

import scrapy
class imdb_project(scrapy.Spider):
name = 'imdb'
start_urls = ['https://www.imdb.com/chart/top']
def parse(self, response):
for i in response.css('.titleColumn'):
title = i.css('a::text').get()
year = i.css('.secondaryInfo::text').get()[1:-1] 
if int(year) >= 2000:
# or you could just do title if you don't
# want the year
print("{0} ({1})".format(title, year))

最新更新