我有以下形式的网址: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