0
以下是代碼。基本上,我正在刮電影信息。來自IMDB.com。但不知何故,請求不會取消對象「addr」中的url。我放入parse_item2中的「打印」根本沒有顯示出來。Scrapy不遵循請求url
這讓我瘋狂。我花了幾個小時。任何有經驗的人都可以幫忙嗎?非常感謝。
# code for the spider
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from scrapy.http import Request, Response
from beta.items import BetaItem
import urllib2
class AlphaSpider(CrawlSpider):
name = 'alpha'
allowed_domains = ['amazon.com','imdb.com']
start_urls = ['http://www.imdb.com/search/title?at=0&sort=boxoffice_gross_us&title_type=feature&year=2005,2005']
rules = (Rule(SgmlLinkExtractor(restrict_xpaths=('//td/a',), allow=('/title/')), callback='parse_item1'),
)
def parse_item1(self, response):
sel = Selector(response)
item = BetaItem()
idb = sel.xpath('//link[@rel="canonical"]/@href').extract()
idb = idb[0].split('/')[-2]
item['idb'] = idb
title = sel.xpath('//h1[@class="header"]/span[@class="itemprop"]/text()').extract()
item['title'] = title
addr = 'http://www.imdb.com/title/' + idb + '/business'
request = Request(addr, callback=self.parse_item2)
request.meta['item'] = item
return request
def parse_item2(self, response):
print 'I am here'
item = response.meta['item']
sel = Selector(response)
# BLA BLA BLA
return item
'parse_item1'工作嗎? 'addr'指向的頁面是否存在? – Blender
嗨攪拌機,是「idb」和「標題」可以被抓取。 – maxwell
由於Scrapy的抓取隊列是LIFO,因此可能需要一段時間才能到達提取的鏈接。你可以在特定頁面上測試它嗎? – Blender