如何使用scrapy从imdb获取“ Production Co”?

时间:2019-01-28 13:18:52

标签: python scrapy

我想从imdb电影中删除Production Co,但我不知道如何。

我不知道我需要检索什么样的信息。现在,我只需要获得电影标题即可。

This is my code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 27 20:10:53 2019

@author: Razva
"""


import scrapy
from imdb2.items import Imdb2Item


class ThirdSpider(scrapy.Spider):
name = "imdbtestspider"
allowed_domains = ["imdb.com"]
start_urls = (
    'http://www.imdb.com/chart/top',
)

def parse(self, response):
    links = response.xpath('//tbody[@class="lister-list"]/tr/td[@class="titleColumn"]/a/@href').extract()
    i =1 
    for link in links:
        abs_url = response.urljoin(link)
        #
        url_next = '//*[@id="main"]/div/span/div/div/div[2]/table/tbody/tr['+str(i)+']/td[3]/strong/text()'
        rating = response.xpath(url_next).extract()
        if (i <= len(links)):
            i=i+1
        yield scrapy.Request(abs_url, callback = self.parse_indetail, meta={'rating' : rating})



def parse_indetail(self,response):
    item = Imdb2Item()
    #
    item['title'] = response.xpath('//div[@class="title_wrapper"]/h1/text()').extract()[0][:-1]

    return item

如果有人可以给我小费,我会很感激的。

1 个答案:

答案 0 :(得分:0)

有关生产公司的名称,请尝试:

def parse_indetail(self,response):
    item = Imdb2Item()
    item['title'] = response.xpath('//div[@class="title_wrapper"]/h1/text()').extract()[0][:-1]
    item['production'] = response.xpath('//h4[contains(text(), "Production Co")]/following-sibling::a/text()').get()
    return item