До того, как моя работа выполнялась следующим образом: в каждой ссылке из топ-250 из imdb она открывалась и получала необходимую мне информацию.
Сейчас у меня есть CSV-файл с 500 ссылками иМне нужно, чтобы открыть один за другим и получить информацию, которую я ищу.Однако я немного растерялся и понятия не имею, как это сделать.Я думал об изменении
def parse(self,response)
Но я не уверен, как
Это мой предыдущий код:
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]
item['production'] = response.xpath('//h4[contains(text(), "Production Co")]/following-sibling::a/text()').extract()
return item
И мой код выглядит так правильносейчас:
import scrapy
from imdb2.items import Imdb2Item
import csv
import re
from scrapy.contrib.linkextractors import LinkExtractor
class ThirdSpider(scrapy.Spider):
name = "imdbtestspider"
allowed_domains = []
with open('links.csv') as f:
start_urls = [url.strip() for url in f.readlines()]
def parse(self, response):
#this should change i guess?
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()').extract()
return item
Я добавил, чтобы получать ссылки из файла csv, но я не знаю, что изменить при разборке по умолчанию.
Спасибо.