2013-08-13 3 views
0

page = agent.get('http://www.print-index.ru/default.aspx?p=81&gr=198')을 통해 링크를 검색하고 그 후에 페이지 링크에 많은 링크가 있지만 어떻게 사용하는지 모릅니다. 기계화. I에 유래에이 방법을 발견Nokogiri 및 Mechanize 도움말 (Nokogiri에서 Mechanize를 통해 링크를 클릭하면 찾아보기)

page = agent.get "http://google.com" 
node = page.search ".//p[@class='posted']" 
Mechanize::Page::Link.new(node, agent, page).click 

를하지만 나는 많은 연결이 방법을 사용할 수 있도록하는 방법을 하나 개의 링크를 사용할 수 있습니다.

추가 정보를 게시해야하는 경우 알려주십시오.

+0

가능한 경우 2-3 링크의 HTML 코드가 필요합니다. –

+0

어떤 html 코드를 사용 하시겠습니까? – user2596615

+0

괜찮아요. 한 가지만 ..'puts node.size'의 답변을 알려주시겠습니까? –

답변

2

당신의 목표는 다음 페이지로 그것을 확인하는 것입니다 그리고 당신이 정말로 걱정하는 모두, 그것은 떨어져 약간의 정보를 긁어 경우 :

  • (데이터를 긁어를 들어)

    당신은 페이지의 콘텐츠에 도착하는 방법이 수행 될 수를 방문해야합니다. 참고로, Mechanize은 장면 뒤에서 Nokogiri를 사용합니다. 파싱 ​​된 페이지에서 요소를 파헤 치기 시작하면 Nokogiri 관련 객체로 돌아옵니다. 이 내 프로젝트 인 경우

    어쨌든, 나는 아마를 검색 한 후 Nokogiri을 페이지의 내용에서 얻을 수 OpenURI를 사용의 길을 갈 것입니다. 추가 종속성을 요구하는 대신 Ruby 표준 라이브러리를 사용하는 것을 좋아합니다.

    다음

    는 예 OpenURI을 사용하고 있습니다 : 나는 구글을 사용

    require 'mechanize' 
    
    agent = Mechanize.new 
    printing_page = agent.get("http://www.print-index.ru/default.aspx?p=81&gr=198") 
    
    # ... 
    # Your code to scrape whatever you want from the Printing Page goes here 
    # ... 
    
    # Find the next page to visit. Example: You want to visit the "About the project" page next 
    about_project_link_in_navbar_menu = printing_page.search('a.graymenu')[4] # This is a overly simple finder. Nokogiri can do xpath searches too. 
    about_project_link_in_navbar_menu_url = "http://www.print-index.ru#{about_project_link_in_navbar_menu.attributes["href"].value}" # Get the URL page 
    
    about_project_page = agent.get(about_project_link_in_navbar_menu_url) 
    
    # .... 
    # Do something... 
    # .... 
    

    PS :

    require 'nokogiri' 
    require 'open-uri' 
    
    printing_page = Nokogiri::HTML(open("http://www.print-index.ru/default.aspx?p=81&gr=198")) 
    
    # ... 
    # Your code to scrape whatever you want from the Printing Page goes here 
    # ... 
    
    # Find the next page to visit. Example: You want to visit the "About the project" page next 
    about_project_link_in_navbar_menu = printing_page.css('a.graymenu')[4] # This is a overly simple finder. Nokogiri can do xpath searches too. 
    about_project_link_in_navbar_menu_url = "http://www.print-index.ru#{about_project_link_in_navbar_menu.attributes["href"].value}" # Get the URL page 
    
    about_project_page = Nokogiri::HTML(open(about_project_link_in_navbar_menu_url)) # Get the About page's content 
    
    # .... 
    # Do something... 
    # .... 
    

    여기 페이지의 콘텐츠를 얻을 수 Mechanize를 사용하는 예제 (그들은 매우 유사)입니다 러시아어를 영어로 번역하는 중 .. 변수 이름이 틀린 경우, 미안 해요! : X

  • +0

    감사합니다. 그것은 작동합니다! – user2596615

    +0

    듣기 좋습니다. –