2016-04-01 19 views
0

Google'da Mechanize kullanarak arama yapan bir program var, ancak program Google'da arama yaparken de http://webcache.googleusercontent.com/ gibi bir şeye benzeyen siteler çekiyor.Bilginin reddedilmesi bir dosyada saklanıyor

Bu sitenin dosyada depolanmasını reddetmek istiyorum. Tüm sitelerin URL'leri farklı şekilde yapılandırılmıştır.

Kaynak kodu:

require 'mechanize' 

PATH = Dir.pwd 
SEARCH = "test" 

def info(input) 
    puts "[INFO]#{input}" 
end 

def get_urls 
    info("Searching for sites.") 
    agent = Mechanize.new 
    page = agent.get('http://www.google.com/') 
    google_form = page.form('f') 
    google_form.q = "#{SEARCH}" 
    url = agent.submit(google_form, google_form.buttons.first) 
    url.links.each do |link| 
    if link.href.to_s =~ /url.q/ 
     str = link.href.to_s 
     str_list = str.split(%r{=|&}) 
     urls_to_log = str_list[1] 
     success("Site found: #{urls_to_log}") 
     File.open("#{PATH}/temp/sites.txt", "a+") {|s| s.puts("#{urls_to_log}")} 
    end 
    end 
    info("Sites dumped into #{PATH}/temp/sites.txt") 
end 

get_urls 

Metin dosyası: Şimdi çalışır

http://www.speedtest.net/ 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:M47_v0xF3m8J 
http://www.speedtest.net/%252Btest%26gbv%3D1%26%26ct%3Dclnk 
http://www.speedtest.net/results.php 
http://www.speedtest.net/mobile/ 
http://www.speedtest.net/about.php 
https://support.speedtest.net/ 
https://en.wikipedia.org/wiki/Test 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:R94CAo00wOYJ 
https://en.wikipedia.org/wiki/Test%252Btest%26gbv%3D1%26%26ct%3Dclnk 
https://www.test.com/ 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:S92tylTr1V8J 
https://www.test.com/%252Btest%26gbv%3D1%26%26ct%3Dclnk 
https://www.speakeasy.net/speedtest/ 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:sCEGhiP0qxEJ:https://www.speakeasy.net/speedtest/%252Btest%26gbv%3D1%26%26ct%3Dclnk 
https://www.google.com/webmasters/tools/mobile-friendly/ 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:WBvZnqZfQukJ:https://www.google.com/webmasters/tools/mobile-friendly/%252Btest%26gbv%3D1%26%26ct%3Dclnk 
http://www.humanmetrics.com/cgi-win/jtypes2.asp 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:w_lAt3mgXcoJ:http://www.humanmetrics.com/cgi-win/jtypes2.asp%252Btest%26gbv%3D1%26%26ct%3Dclnk 
http://speedtest.xfinity.com/ 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:snNGJxOQROIJ:http://speedtest.xfinity.com/%252Btest%26gbv%3D1%26%26ct%3Dclnk 
https://www.act.org/content/act/en/products-and-services/the-act/taking-the-test.html 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:1sMSoJBXydo 
https://www.act.org/content/act/en/products-and-services/the-act/taking-the-test.html%252Btest%26gbv%3D1%26%26ct%3Dclnk 
https://www.16personalities.com/free-personality-test 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:SQzntHUEffkJ 
https://www.16personalities.com/free-personality-test%252Btest%26gbv%3D%26%26ct%3Dclnk 
https://www.xamarin.com/test-cloud 
http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:ypEu7XAFM8QJ: 
https://www.xamarin.com/test-cloud%252Btest%26gbv%3D1%26%26ct%3Dclnk 
+0

'if ifadesi 'ekleme hakkında ne dersiniz? – 7urkm3n

+0

Ve sayfa webcache ile eşleşiyorsa bir 'skip 'kullanılıyor mu? – 13aal

+0

Evet, bunun gibi bir şey (1..10) .each {| a | Sonraki a.even? , numarasını koyar " – 7urkm3n

cevap

1

. Ben success('log') ile sorun vardı, neden bilmiyorum ama yorumladı.

str_list = str.split(%r{=|&}) 
    next if str_list[1].split('/')[2] == "webcache.googleusercontent.com" 
    # success("Site found: #{urls_to_log}") 
    File.open("#{PATH}/temp/sites.txt", "a+") {|s| s.puts("#{urls_to_log}")} 
+0

Bu iyi bir cevap, mükemmel çalışıyor, başarının lol 'def başarısı olduğunu ifade etmeyi unutmuşum (giriş)' '' '' '' [BAŞARILAR] [SÜREÇ] # {input} ''' '' '' '' '' '' '' bittiğini bilmiyorum – 13aal

+0

'. Başarının ('log') 'mechanize' ile geldiğini düşündüm. Sorun değil. – 7urkm3n

1

çok kullanmak parçalarının içine URL'ler sökmeye için kullanılan, iyi test tekerlekleri vardır. Yakut bize kolayca ayıklamak için izin veren URI ile gelir host, path veya query: Ruby'nin Enumerable modül bir dizi veya enumerable nesnenin üzerine döngü kolaylaştırır ve reddetmek veya elemanları seçmek reject ve select içerir

require 'uri' 

URL = 'http://foo.com/a/b/c?d=1' 

URI.parse(URL).host 
# => "foo.com" 
URI.parse(URL).path 
# => "https://stackoverflow.com/a/b/c" 
URI.parse(URL).query 
# => "d=1" 

o: Eğer alt dizeleri bir URL dizi kontrol ve reddetmek olabilir tümünün kullanılması

(1..3).select{ |i| i.even? } # => [2] 
(1..3).reject{ |i| i.even? } # => [1, 3] 

herhangi istemediğiniz:

require 'uri' 

%w[ 
    http://www.speedtest.net/ 
    http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:M47_v0xF3m8J 
].reject{ |url| URI.parse(url).host[/googleusercontent\.com$/] } 
# => ["http://www.speedtest.net/"] 

Bu yöntemleri ve teknikleri kullanarak bir giriş dosyasını reddedebilir veya seçebilir veya yalnızca tek URL'lere göz atabilir ve bunları yok saymayı veya onlardan yararlanmayı seçebilirsiniz.

+0

Daha fazla "URI" ve "Nokogiri" kullanmaktan hoşlanıyorum ama asla "mechanize" hakkında hiçbir şey bilmiyorum. – 7urkm3n