スクレイピングを試すために映画.comというページをスクレイピングして映画のタイトルなどをデータベースに保存するコードを書いてみたのですが、全490件くらいのうち250件くらいでスクレイピングが終わってしまうのですが、コードを見直しても間違えている点が見当たりません。
以下私が書いたコードになります
lang
1class Scraping 2 def self.movie_urls 3 agent = Mechanize.new 4 links = [] 5 current_page = agent.get("http://eiga.com/now/") 6 while true 7 elements = current_page.search('.m_unit h3 a') 8 elements.each do |ele| 9 links << ele[:href] 10 end 11 next_link = current_page.at('.next_page') 12 next_url = next_link.get_attribute('href') 13 unless next_url 14 break 15 end 16 current_page = agent.get('http://eiga.com' + next_url) 17 end 18 links.each do |link| 19 get_product('http://eiga.com' + link) 20 end 21 end 22 23 def self.get_product(link) 24 agent = Mechanize.new 25 page = agent.get(link) 26 title = page.at('.moveInfoBox h1').inner_text 27 image_url = page.at('.pictBox a img')[:src] if page.at('.pictBox img') 28 product = Product.where(:title => title, :image_url => image_url).first_or_initialize 29 product.save 30 end 31end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。