rubyで日経一部上場企業の一覧を取得する。
http://www.jpubb.com/ さまにアクセスさせていただいています。
require 'rubygems' require 'hpricot' require 'mechanize' require 'kconv' require 'uri' $KCODE="u" str3="http://www.jpubb.com/list/list.php?se=tou1&pageID=" agent = Mechanize.new agent.user_agent_alias="Windows IE 7" 35.times(){|i| #1.times(){|i| # puts i+1; str3="http://www.jpubb.com/list/list.php?se=tou1&pageID="+i.to_s #str3="http://www.jpubb.com/list/list.php?se=toum&pageID="+i.to_s #str3="http://www.jpubb.com/list/list.php?se=tou2&pageID="+i.to_s response=agent.get(str3); doc=Hpricot(response.body); j=(doc/"table.corpTable") #puts j; k=1; link123=""; (j/"tr").each{|f| # puts k; name=(f/"td.name").inner_text; code=(f/"td.code").inner_text; other=(f/"td.other").inner_text; #other2=(((f/"td.other")/"a")[3]); other2=(((f/"td.other")/"a")); other2.each(){|link| link123=link[:href] } other=other.split("|")[0,3].join("\t") #puts other2; #link1=((f/"td.other")/"a")[3]; puts name+"\t"+code+"\t"+other+"\t"+link123.to_s; k=k+1; } }
東証マザーズ。変なバグを取ったバージョン。
require 'rubygems' require 'hpricot' require 'mechanize' require 'kconv' require 'uri' $KCODE="u" #str2=URI.encode(text.toutf8) #str3="http://thesaurus.weblio.jp/content/"+str2; # str3="http://www.jpubb.com/list/list.php?se=toum&pageID=" agent = Mechanize.new agent.user_agent_alias="Windows IE 7" k=1; 5.times(){|i| #1.times(){|i| # puts i+1; str3="http://www.jpubb.com/list/list.php?se=toum&pageID="+k.to_s response=agent.get(str3); doc=Hpricot(response.body); j=(doc/"table.corpTable") #puts j; link123=""; (j/"tr").each{|f| # puts k; name=(f/"td.name").inner_text; code=(f/"td.code").inner_text; other=(f/"td.other").inner_text; #other2=(((f/"td.other")/"a")[3]); other2=(((f/"td.other")/"a")); other2.each(){|link| link123=link[:href] } other=other.split("|")[0,3].join("\t") #puts other2; #link1=((f/"td.other")/"a")[3]; if name.length !=0 then puts name+"\t"+code+"\t"+other+"\t"+link123.to_s; k=k+1; end } }