2
Scrapy和Selenium玩的很好,如果一切都在相同的功能parse()
,但因爲我需要在解析函數內添加更多的代碼我想將分割的部分拆分功能parse_data()
並使用請求()但回調不起作用。Scrapy Selenium無法提出請求()回調
class MySpider(Spider):
name = "myspider"
start_urls = ["http://example.com/Data.aspx",]
def __init__(self, *args, **kwargs):
super(MySpider, self).__init__(*args, **kwargs)
self.driver = webdriver.Firefox()
dispatcher.connect(self.spider_closed, signals.spider_closed)
def spider_closed(self, spider):
self.driver.close()
def parse(self, response):
item = MyItem()
self.driver.get(response.url)
sel = Selector(response)
buttons = len(self.driver.find_elements_by_xpath("//input[@class='buttonRowDetails']"))
for x in range(buttons):
time.sleep(5)
button = self.driver.find_elements_by_xpath("//input[@class='buttonRowDetails']")[x]
button.click()
time.sleep(5)
response = TextResponse(url=self.driver.current_url, body=self.driver.page_source, encoding='utf-8')
print '\n\n\nHELLO FROM PARSE'
yield Request(response.url, meta={'item': item}, callback=self.parse_data)
def parse_data(self, response):
item = response.meta['item']
print '\n\nHELLO FROM PARSE_DATA'