2012-02-20 68 views
2

我使用機械化和引入nokogiri解析這兩個站點上的某些樂透結果(它們很相似): http://www1.caixa.gov.br/loterias/loterias/lotofacil/lotofacil_resultado.asp http://lotofacil.resultadoloteria.org/紅寶石機械化沒有得到充分的內容

這裏是我的代碼:

require 'nokogiri' 
require 'mechanize' 

agent = Mechanize.new 
agent.user_agent_alias = 'Mac Safari' 
page = agent.get('http://lotofacil.resultadoloteria.org/') 
doc = Nokogiri::HTML(page.body) 
doc.xpath('//table[@class="tabela_jogo"]//span').each { |value| puts value } 

第二現場工作正常。結果:

<span id="lfacil1">01</span> 
<span id="lfacil2">03</span> 
<span id="lfacil3">05</span> 
<span id="lfacil4">08</span> 
<span id="lfacil5">10</span> 
<span id="lfacil6">11</span> 
<span id="lfacil7">13</span> 
<span id="lfacil8">14</span> 
<span id="lfacil9">15</span> 
<span id="lfacil10">18</span> 
<span id="lfacil11">20</span> 
<span id="lfacil12">22</span> 
<span id="lfacil13">23</span> 
<span id="lfacil14">24</span> 
<span id="lfacil15">25</span> 

但我從第一個拿不到樂透號碼。這裏的結果:

<span id="lfacil1"></span> 
<span id="lfacil2"></span> 
<span id="lfacil3"></span> 
<span id="lfacil4"></span> 
<span id="lfacil5"></span> 
<span id="lfacil6"></span> 
<span id="lfacil7"></span> 
<span id="lfacil8"></span> 
<span id="lfacil9"></span> 
<span id="lfacil10"></span> 
<span id="lfacil11"></span> 
<span id="lfacil12"></span> 
<span id="lfacil13"></span> 
<span id="lfacil14"></span> 
<span id="lfacil15"></span> 
<span id="lfacil1_2"></span> 
<span id="lfacil2_2"></span> 
<span id="lfacil3_2"></span> 
<span id="lfacil4_2"></span> 
<span id="lfacil5_2"></span> 
<span id="lfacil6_2"></span> 
<span id="lfacil7_2"></span> 
<span id="lfacil8_2"></span> 
<span id="lfacil9_2"></span> 
<span id="lfacil10_2"></span> 
<span id="lfacil11_2"></span> 
<span id="lfacil12_2"></span> 
<span id="lfacil13_2"></span> 
<span id="lfacil14_2"></span> 
<span id="lfacil15_2"></span> 

我覺得是一些與機械化,因爲p page.body返回而不樂透號碼太多的內容。有任何想法嗎?

謝謝。 :)

回答

2

那是因爲他們不在那裏。我發現他們對你雖然:

page = agent.get('http://www1.caixa.gov.br/loterias/loterias/lotofacil/lotofacil_pesquisa_new.asp') 
numbers = page.body.split('|')[3..17] 

代替,這也:

doc = Nokogiri::HTML(page.body) 

機械化已經慎重了,給你:

doc = page.parser 
+0

非常感謝!但你是如何找到那個頁面的? – Hodes 2012-02-20 03:46:43

+0

不客氣。我用charles proxy打開chrome加載它,然後搜索charles中的數字。 – pguardiario 2012-02-20 04:01:24

+0

再次感謝。 :) – Hodes 2012-02-20 22:56:16