2011-09-08 3 views
3

방문 실제로 링크를 클릭하십시오.루비 말미잘 거미는 I가 크롤링이 설정 한

거미를 한 번 사용하고 URL을 모두 저장하고 WATIR을 사용하여 태그를 추가 할 수는 있지만 속도가 느리고 skip_links_like 및 페이지 깊이 기능이 좋으므로이 문제를 방지하고 싶습니다.

어떻게 구현할 수 있습니까?

답변

3

URL을로드하기 전에 URL에 뭔가를 추가 하시겠습니까? 당신은 focus_crawl을 사용할 수 있습니다.

Anemone.crawl("http://www.website.co.uk", :depth_limit => 1) do |anemone| 
    anemone.focus_crawl do |page| 
     page.links.map do |url| 
      # url will be a URI (probably URI::HTTP) so adjust 
      # url.query as needed here and then return url from 
      # the block. 
      url 
     end 
    end 
    anemone.on_every_page do |page| 
     puts page.url 
    end 
end 

의 URL 목록을 필터링 할 의도 focus_crawl 방법 :

는 각 페이지에 따라 링크를 선택할 것 블록을 지정합니다. 블록은 URI 객체의 배열을 반환해야합니다.

하지만 범용 URL 필터로도 사용할 수 있습니다. 당신은 모든 링크에 atm_source=SiteCon&atm_medium=Mycampaign를 추가하고자한다면

예를 들어, 다음 page.links.map은 다음과 같이 보일 것입니다 :

page.links.map do |uri| 
    # Grab the query string, break it into components, throw out 
    # any existing atm_source or atm_medium components. The to_s 
    # does nothing if there is a query string but turns a nil into 
    # an empty string to avoid some conditional logic. 
    q = uri.query.to_s.split('&').reject { |x| x =~ /^atm_(source|medium)=/ } 

    # Add the atm_source and atm_medium that you want. 
    q << 'atm_source=SiteCon' << 'atm_medium=Mycampaign' 

    # Rebuild the query string 
    uri.query = q.join('&') 

    # And return the updated URI from the block 
    uri 
end 

당신이 atm_source 또는 atm_medium 비 URL 안전 문자가 포함 된 다음 URI 인코딩 경우 그들.

+0

예를 들어 주시겠습니까? 각 웹 사이트에 "thisstring"을 추가하고 싶다면? – Benjamin

+0

@Benjamin : URL의 어떤 부분에 추가 하시겠습니까? CGI 매개 변수로? –

+0

예 : "http : //yahoo.co.uk/?atm_source=SiteCon&atm_medium=Mycampaign"여기에 방문한 각 URL에 "? atm_source = SiteCon & atm_medium = Mycampaign"을 추가하고 싶습니다. – Benjamin