imdb 前 250 部电影抓取工具——如何仅从 2000 年及以后获得结果?
imdb top 250 movies scraper -- how to get results only from year 2000 and up?
我是 Python(和一般编码)和 scrapy 的新手,所以我对两者的了解是 basic/limited(我基本上只是从各种 Google 搜索中复制代码)。
到目前为止,我设法想出了一个有效的代码:
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 年及以后上映的电影的片名。
您可以看到 here 年份显示在电影标题之后。这应该很容易做到,但对于我来说,我在 Google 甚至 Stack Overflow 上都找不到类似的例子来帮助我开始解决这个问题。
我认为它应该像这样简单:
if movie_year >= 2000:
then run the 'for' loop above
...但我不知道如何在 Python 中编写上述代码。任何帮助(如果可能,请不要使用正则表达式或 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))
我是 Python(和一般编码)和 scrapy 的新手,所以我对两者的了解是 basic/limited(我基本上只是从各种 Google 搜索中复制代码)。
到目前为止,我设法想出了一个有效的代码:
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 年及以后上映的电影的片名。
您可以看到 here 年份显示在电影标题之后。这应该很容易做到,但对于我来说,我在 Google 甚至 Stack Overflow 上都找不到类似的例子来帮助我开始解决这个问题。
我认为它应该像这样简单:
if movie_year >= 2000:
then run the 'for' loop above
...但我不知道如何在 Python 中编写上述代码。任何帮助(如果可能,请不要使用正则表达式或 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))