擅长:python、mysql、java
<p>您的spider需要一个<code>parse</code>方法,它是所有初始请求的默认回调。您只需将<code>parse_url</code>方法重命名为<code>parse</code>,它就可以正常工作了。</p>
<pre><code>class IkeaSpider(scrapy.Spider) :
name = "Ikea"
allower_domains = ["http://www.ikea.com/"]
start_urls = ["http://www.ikea.com/fr/fr/catalog/productsaz/8/"]
def parse(self, response):
for sel in response.xpath('//div[@id="productsAzLeft"]'):
base_url = 'http://www.ikea.com/'
follow_url = sel.xpath('//span[@class="productsAzLink"]/@href').extract()
complete_url = urlparse.urljoin(base_url, follow_url)
request = Request(complete_url, callback = self.parse_page)
yield request
</code></pre>
<h3>替代品</h3>
<p>您还可以定义一个<code>start_requests</code>方法,并像在这里一样使用一个定义的<code>callback</code>参数手动生成<code>scrapy.Requests</code>。</p>