原因は#ほんとは上記コメントアウトの2行を加えたいのですが、処理が追いつかないのかな?下に記しました
のコード辺りだと思うのですが、効率のいい書き方(全体的に)がありましたらご教授お願いします。
エラー概要
xmlXPathCompOpEval: function content not found (RuntimeError)
lang
1#coding : utf-8 2require 'kconv' 3require 'open-uri' 4require 'mechanize' 5require 'csv' 6 7agent = Mechanize.new 8header = ["会社名","メールアドレス","代表者名","所在地","TEL","FAX"] 9CSV.open("XXX.csv","w", :headers => header, :write_headers => true) do |csv| 10 #47都道府県 11 for num in 1..47 do 12 if num < 2 then 13 pi = 1 14 elsif num < 8 then 15 pi = 2 16 elsif num < 15 then 17 pi = 3 18 elsif num < 25 then 19 pi = 4 20 elsif num < 31 then 21 pi = 5 22 elsif num < 36 then 23 pi = 6 24 elsif num < 40 then 25 pi = 7 26 else num < 48 27 pi = 8 28 end 29 url = "http://XXXXXXXXXXXXX" + pi.to_s + "&pref_id=" + num.to_s 30 31 html = open(url,"r:EUC-JP") 32 page = Nokogiri.HTML(html,nil,"EUC-JP") 33 count = 0 34 @company = [] 35 #1ページに30社 36 while count < 30 do 37 #個別会社ページの要素を1つずつ取得 38 page.search(".mini2+ .mini2 a").each do |kaisya_search| 39 sleep(1) 40 count += 1 41 kaisya_url = kaisya_search[:href] 42 kaisya_html = open(kaisya_url,"r:EUC-JP") 43 kaisya_page = Nokogiri.HTML(kaisya_html,nil,"EUC-JP") 44 #tr:nth-child(1)もしくはtr:nth-child(3)のCSS3にメールアドレス 45 valid_address = /[\w\d_-]+@[\w\d_-]+.[\w\d._-]+/ 46 mail_address = kaisya_page.search('tr:nth-child(1) a').text 47 if valid_address =~ mail_address 48 @company.push(kaisya_search.text.toutf8) 49 @company.push(kaisya_page.search('tr:nth-child(1) a').to_s.scan(/[\w\d_-]+@[\w\d_-]+.[\w\d._-]+/)[1]) 50 @company.push(kaisya_page.search('.grid-7 tr:nth-child(2) td').text) 51 @company.push(kaisya_page.search('.grid-7 tr:nth-child(3) td').text) 52 #@company.push(kaisya_page.search('.grid-7 tr:nth-child(4) td').text) 53 #@company.push(kaisya_page.search(':content("ファックス")+ td').text) 54 55 #ほんとは上記コメントアウトの2行を加えたいのですが、処理が追いつかないのかな?下に記しました 56 57 else 58 @company.push(kaisya_search.text.toutf8) 59 @company.push(kaisya_page.search('tr:nth-child(3) a').to_s.scan(/[\w\d_-]+@[\w\d_-]+.[\w\d._-]+/)[1]) 60 @company.push(kaisya_page.search('tr:nth-child(4) .standard').text) 61 @company.push(kaisya_page.search('tr:nth-child(6) .standard').text) 62 #@company.push(kaisya_page.search('td td tr:nth-child(8) .standard').text) 63 #@company.push(kaisya_page.search('tr:nth-child(10) .standard').text) 64 65 #同上 66 67 end 68 csv << @company.to_a.pop(4)#daihyousya,syozaichi,tel,fax, 69 #次のページに移動、 70 if count == 30 71 unless page.search("span+ a").empty? 72 next_30_url = page.search("span+ a")[0][:href] 73 next_30_html = "http://www.akabou.ne.jp/" + next_30_url 74 next_html = open(next_30_html,"r:EUC-JP") 75 page = Nokogiri.HTML(next_html,nil,"EUC-JP") 76 count = 0 77 sleep(1) 78 end 79 end 80 end 81 end 82 end 83end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。