BARCELONA (Reuters) - Lewis Hamilton took pole position at the Spanish Grand Prix for the second successive year on Saturday with Ferrari's championship leader Sebastian Vettel just missing out but joining the Mercedes driver on the front row.
The pole was the 64th of the Briton's career, one short of his late Brazilian boyhood idol Ayrton Senna on the all-time list and with record holder Michael Schumacher's 68 in his sights.
It was also the 250th by a British driver in Formula One.
All but three of the last 16 races in Spain have been won from the top slot, leaving Hamilton with every chance of taking his second victory of the season on Sunday to cut Vettel's 13-point lead.
"I'm super proud that we can get back up there. My last lap was so-so but I could see the fans cheering and all the flags," said the triple champion, interviewed on the finish line in front of the crowd in a new departure for the sport.
Hamilton's team mate Valtteri Bottas, a first-time winner in Russia two weeks ago, qualified third, with Ferrari's Kimi Raikkonen fourth.
Dutch teenager Max Verstappen, who became Formula One's youngest winner in Spain last year after Hamilton and then team mate and 2016 champion Nico Rosberg collided at the start, will line up fifth for Red Bull.
Vettel's car needed an engine change between final practice and qualifying, and he praised his mechanics for their efforts in getting it all done with minutes to spare.
"They did an engine change in sub two hours. It's a miracle they got me out," he said.
On a rollercoaster afternoon, the German missed out on a second successive pole by a mere 0.051 of a second after locking up and running wide at the final chicane. "I had it, I had it," he said ruefully over the radio.
He could at least thank his lucky stars, or years of experience, to get that far after being told to stop the car soon after qualifying had started.
The four times world champion hesitated to obey the instruction, however. "Seems better now," he said and carried on. "OK, box, Sebastian, box," he was told.
"OK, you can push," came the eventual radio message from the pit wall, with the problem apparently resolved. He then set the fastest lap of the session before Hamilton and Raikkonen went quicker.
Double world champion Fernando Alonso, whose McLaren broke down in first practice and was slowest overall on Friday, enjoyed a miraculous transformation -- and delighted his home crowd -- by qualifying an astonishing seventh.
"Maybe it was at the oval (where) I learnt how to go quick on the straights," said the Spaniard, who will leave Barcelona on Sunday night for Indianapolis to compete in the Indy 500 on May 28. "P7 is a gift and we will see what we can do tomorrow.
"Sometimes the weekends start the wrong way but then they fix themselves, and vice versa. The important thing is tomorrow, to try to get a few points," he added.
"It was better than expected but the support from the people gives you a few extra tenths."
Australian Daniel Ricciardo was sixth for Red Bull, with Mexican Sergio Perez eighth in a Force India.
Britain's Jolyon Palmer, yet to score a point this season, had another disappointing Saturday after qualifying 17th in the Renault while team mate Nico Hulkenberg went through to the second phase and will start 13th.
"Yesterday felt good, today I've just struggled," said Palmer, whose results have left him feeling the pressure.
"It's tough. I really thought this weekend could be good but today it's all come crashing down."
Canadian 18-year-old rookie Lance Stroll, also yet to finish in the points after four races, failed to get his Williams through the first phase and will line up 18th. Brazilian team mate Felipe Massa will start ninth.
(Reporting by Alan Baldwin, editing by Toby Davis)