我有以下形式的網址:scrapy - 這是分頁解析項目
example.com/foo/bar/page_1.html
總共有53頁是,他們每個人都有〜20行。
我基本上想要從所有頁面獲取所有行,即〜53 * 20個項目。
我在parse方法工作的代碼,它分析一個網頁,並且還進入每個項目一個頁面更深,以獲取有關該項目的詳細信息:
def parse(self, response):
hxs = HtmlXPathSelector(response)
restaurants = hxs.select('//*[@id="contenido-resbus"]/table/tr[position()>1]')
for rest in restaurants:
item = DegustaItem()
item['name'] = rest.select('td[2]/a/b/text()').extract()[0]
# some items don't have category associated with them
try:
item['category'] = rest.select('td[3]/a/text()').extract()[0]
except:
item['category'] = ''
item['urbanization'] = rest.select('td[4]/a/text()').extract()[0]
# get profile url
rel_url = rest.select('td[2]/a/@href').extract()[0]
# join with base url since profile url is relative
base_url = get_base_url(response)
follow = urljoin_rfc(base_url,rel_url)
request = Request(follow, callback = parse_profile)
request.meta['item'] = item
return request
def parse_profile(self, response):
item = response.meta['item']
# item['address'] = figure out xpath
return item
的問題是,我該怎麼辦抓取每個頁面?
example.com/foo/bar/page_1.html
example.com/foo/bar/page_2.html
example.com/foo/bar/page_3.html
...
...
...
example.com/foo/bar/page_53.html
start_urls想法很棒。非常感謝 – AlexBrand
優秀的答案。非常感謝。 scrapy網站上的LinkExtractor並不適合我。這樣做。 –
如何檢查頁面是否找不到。它只有53頁。但如果我叫'xrange(1,60)'。 – user1586957