2016-06-02 3 views
0

기본 검색에서 돌아온 모든 페이지의 모든 Uni 이름을 표시하기 위해 UCAS 웹 사이트에서 데이터를 긁어 내려고했습니다. 이것은 내 코드모든 검색 결과 페이지의 정보를 모으는 데이터

"The University of Aberdeen 
Abertay University 
Aberystwyth University 
ABI College 
Abingdon and Witney College 
The Academy of Contemporary Music 
Access to Music 
Accrington & Rossendale College 
Activate Learning (Oxford, Reading, Banbury & Bicester) 
The College of Agriculture, Food and Rural Enterprise 
Amersham & Wycombe College 
Amsterdam Fashion Academy 
Anglia Ruskin University 
Anglo European College of Chiropractic 
Arden University (RDI) 
University of the Arts London 
Arts University Bournemouth (formerly University College) 
ARU London 
Askham Bryan College 
Aston University, Birmingham 
Availability 
Applying through Extra 
Single/Combined subjects 
Provider types 
How you study 
Qualification level 
Conservatoire specialism" 

입니다 : 아래 볼 수 있듯이

지금까지, 루프 작동하지 않고, 그것은 페이지 하나뿐만 아니라 어떤 임의의 정보의 모든 대학의 이름을 표시

require 'rubygems' 
require 'nokogiri' 
require 'open-uri' 
require 'mechanize' 

mechanize = Mechanize.new 

doc = mechanize.get('http://search.ucas.com/') 

form = doc.forms.first 

form['Vac'] = '2' 
form['AvailableIn'] = '2016' 
doc = form.submit 
doc.search('li.results clearfix').each do |h3| 
    puts h3.text.strip 


    while a = doc.at('div.pagerclearfix a') 
    doc = Nokogiri::HTML(open(a[:href]))  
    doc.search('results clearfix').each do |h3|  
     puts h3.text.strip 

    end 
    end 
end 
+0

정확히 무엇이 문제입니까? 모든 페이지가 아닌 첫 페이지의 결과 만 표시됩니까? – mlovic

+0

그래, 첫 번째 puts가 인쇄되고 루프가 제대로 작동하지 않는 것 같아요. div.pagerclearfix a와 관련된 문제는 페이지를 검사 할 때 pager.clearfix라고 불리는 것 같습니다. – Akamaru

+0

안녕하세요. "[mcve]"를 읽으십시오. 질문 자체에 최소한의 HTML 샘플이 필요합니다. 도움을 요청하려는 일부 사람들은 인터넷에 접속할 수 없거나 문제의 태그를 찾기 위해 큰 파일을 탐색하려고하지 않을 것입니다. –

답변

0

반 패턴이므로 require 'rubygems'은 필요하지 않습니다. Mechanize가 필요하기 때문에 require 'nokogiri'이 필요하지 않으며 OpenURI가 필요하지 않습니다.

pagerclearfix은 별도의 클래스이므로 div.pagerclearfix 선택기가 아무 것도 맞지 않으므로 페이지 매김이 작동하지 않습니다. 또한 while 루프가 잘못된 위치에 있으며 결과를 인쇄하는 each 루프 안에 있으면 안됩니다. 당신이와 끝까지해야합니까

이 같은 것입니다 :

당신이 일반적으로 가장 간단합니다 "다음 페이지"링크를 검색 페이지 매김을 구현할 수있는 다양한 방법이 있습니다
require 'mechanize' 

mechanize = Mechanize.new 

page = mechanize.get('http://search.ucas.com/') 

form = page.forms.first 
form['Vac'] = '2' 
form['AvailableIn'] = '2016' 

page = form.submit 

page.search('li.result h3').each do |h3| 
    puts h3.text.strip 
end 

while next_page_link = page.at('.pager a[text()=">"]') 
    page = mechanize.get(next_page_link['href']) 

    page.search('li.result h3').each do |h3| 
    puts h3.text.strip 
    end 
end 

.

+0

고맙습니다. 완벽하게 작동했습니다. :) – Akamaru