2017-09-20 10 views
0

며칠 전 나는이 질문 : scrapy getting values from multiple sitesscrapy는

을 나는 WEBSITE2에 WEBSITE1에서 값을 전달하는 방법을 배웠다. 이것은 두 사이트의 수익률 정보를 허용합니다. 10 개의 다른 사이트와 같은 경우에는 해결되지 않습니다.

나는 함수에서 함수로 값을 전달할 수 있지만 바보 같다. 보다 효율적인 방법은 정보를 구문 분석 함수에 전달하여 거기에서 산출하는 것입니다. 여기 은 내가 달성하고자하는 의사 코드입니다.

import scrapy 

class GotoSpider(scrapy.Spider): 
    name = 'goto' 
    allowed_domains = ['first.com', 'second.com', 'third.com'] 
    start_urls = ['http://first.com/'] 

def parse(self, response): 
    name = response.xpath(...) 
    price1 = scrapy.Request(second.com, callback = self.parse_check) 
    price2 = scrapy.Request(third.com, callback = self.parse_check2) 
    yield(name, price1, price2) 


def parse_check(self, response): 
    price = response.xpath(...) 
    return price 

def parse_check(self, response): 
    price = response.xpath(...) 
    return price 

답변

2

체크 아웃했을 가능성이 있습니다. 예를 들어 다음과 같이됩니다.

import scrapy 
from inline_requests import inline_requests 

class GotoSpider(scrapy.Spider): 
    name = 'goto' 
    allowed_domains = ['first.com', 'second.com', 'third.com'] 
    start_urls = ['http://first.com/'] 

    @inline_requests 
    def parse(self, response): 
     name = response.xpath(...) 

     response1 = yield scrapy.Request(second.com) 
     price1 = response1.xpath(...) 
     response2 = yield scrapy.Request(third.com) 
     price2 = response2.xpath(...) 

     yield dict(name, price1, price2) 
+0

흠집이 쉽게 들립니다. 하지만 왜 함수가 내장되어 있지 않습니까? 나는 몇몇 사이트를 긁어 내고 이동 중에도 가격을 비교하는 것이 일반적인 생각이라고 생각합니다. – daniel

+0

정확하게 기억한다면 Github을 Scrapy에 통합하는 것에 대한 논의가 있었지만 세부 사항은 기억하지 못했습니다. 이것은 개발자에게 더 많은 질문입니다. –