{"id":252583,"date":"2015-03-06T18:51:09","date_gmt":"2015-03-06T14:51:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=252583"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=252583","title":{"rendered":"\u0412\u0435\u0431-\u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u043d\u0430 Ruby"},"content":{"rendered":"<p> \t\t\t<i>\u042d\u0442\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/www.chrismytton.uk\/2015\/01\/19\/web-scraping-with-ruby\/\">\u00abWeb Scraping with Ruby\u00bb<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u043d\u0430\u0448\u0435\u043b \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u043f\u0440\u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0438 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Ruby. \u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u043c\u0435\u043d\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u0432 \u043b\u0438\u0447\u043d\u044b\u0445 \u0446\u0435\u043b\u044f\u0445. \u041c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u044d\u0442\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043d\u0430\u0432\u044b\u043a, \u043d\u043e \u0438 \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u044f\u0437\u044b\u043a.<\/i> <br \/>  <a name=\"habracut\"><\/a><br \/>  \u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0432\u0435\u0431\u0430 \u043d\u0430 Ruby \u043b\u0435\u0433\u0447\u0435, \u0447\u0435\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0443\u043c\u0430\u0442\u044c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430: \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043e\u0442\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 JSON \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0438\u043b\u044c\u043c\u043e\u0432 \u0441 \u0441\u0430\u0439\u0442\u0430 <a href=\"http:\/\/www.cubecinema.com\/programme\">\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0433\u043e \u043a\u0438\u043d\u043e\u0442\u0435\u0430\u0442\u0440\u0430<\/a>.<\/p>\n<p>  \u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u043a\u0430\u0447\u0430\u0442\u044c html \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043e \u0444\u0438\u043b\u044c\u043c\u0430\u0445. \u0412 Ruby \u0435\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 http \u043a\u043b\u0438\u0435\u043d\u0442, <code>Net::HTTP<\/code>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u043d\u0430\u0434 \u043d\u0438\u043c \u2014 <code>open-uri<\/code><sup>1<\/sup>. \u0418\u0442\u0430\u043a, \u043f\u0435\u0440\u0432\u0430\u044f \u0432\u0435\u0449\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u2014 \u044d\u0442\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c html \u0441 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<pre><code class=\"ruby\">require 'open-uri'  url = 'http:\/\/www.cubecinema.com\/programme' html = open(url) <\/code><\/pre>\n<p>  \u041e\u0442\u043b\u0438\u0447\u043d\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0430\u0440\u0441\u0438\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0438\u0437 \u043d\u0435\u0435. \u041b\u0443\u0447\u0448\u0438\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u2014 <a href=\"http:\/\/www.nokogiri.org\/\">Nokogiri<\/a>. \u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 Nokogiri \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e html, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043a\u0430\u0447\u0430\u043b\u0438. <\/p>\n<pre><code class=\"ruby\">require 'nokogiri'  doc = Nokogiri::HTML(html) <\/code><\/pre>\n<p>  Nokogiri \u043a\u0440\u0443\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a html \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f CSS \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u044b, \u0447\u0442\u043e, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c xpath.<\/p>\n<p>  \u041e\u043a, \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u0438\u043d\u043e\u0444\u0438\u043b\u044c\u043c\u043e\u0432. \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0442\u0430\u043a\u0443\u044e html \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435.<\/p>\n<pre><code class=\"html\">&lt;div class=&quot;showing&quot; id=&quot;event_7557&quot;&gt;   &lt;a href=&quot;\/programme\/event\/live-stand-up-monty-python-and-the-holy-grail,7557\/&quot;&gt;     &lt;img src=&quot;\/media\/diary\/thumbnails\/montypython2_1.png.500x300_q85_background-%23FFFFFF_crop-smart.jpg&quot; alt=&quot;Picture for event Live stand up + Monty Python and the Holy Grail&quot;&gt;   &lt;\/a&gt;   &lt;span class=&quot;tags&quot;&gt;&lt;a href=&quot;\/programme\/view\/comedy\/&quot; class=&quot;tag_comedy&quot;&gt;comedy&lt;\/a&gt; &lt;a href=&quot;\/programme\/view\/dvd\/&quot; class=&quot;tag_dvd&quot;&gt;dvd&lt;\/a&gt; &lt;a href=&quot;\/programme\/view\/film\/&quot; class=&quot;tag_film&quot;&gt;film&lt;\/a&gt; &lt;\/span&gt;   &lt;h1&gt;     &lt;a href=&quot;\/programme\/event\/live-stand-up-monty-python-and-the-holy-grail,7557\/&quot;&gt;       &lt;span class=&quot;pre_title&quot;&gt;Comedy Combo presents&lt;\/span&gt;       Live stand up + Monty Python and the Holy Grail       &lt;span class=&quot;post_title&quot;&gt;Rare screening from 35mm!&lt;\/span&gt;     &lt;\/a&gt;   &lt;\/h1&gt;   &lt;div class=&quot;event_details&quot;&gt;     &lt;p class=&quot;start_and_pricing&quot;&gt;       Sat 20 December | 19:30       &lt;br&gt;     &lt;\/p&gt;     &lt;p class=&quot;copy&quot;&gt;Brave (and not so brave) Knights of the Round Table! Gain shelter from the vicious chicken of Bristol as we gather to bear witness to this 100% factually accurate retelling ... [&lt;a class=&quot;more&quot; href=&quot;\/programme\/event\/live-stand-up-monty-python-and-the-holy-grail,7557\/&quot;&gt;more...&lt;\/a&gt;]&lt;\/p&gt;   &lt;\/div&gt; &lt;\/div&gt; <\/code><\/pre>\n<p>  <\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 html<\/h3>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u0444\u0438\u043b\u044c\u043c \u0438\u043c\u0435\u0435\u0442 css \u043a\u043b\u0430\u0441\u0441 <code>.showing<\/code>, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0448\u043e\u0443 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0438\u0445 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438.<\/p>\n<pre><code class=\"ruby\">showings = [] doc.css('.showing').each do |showing|   showing_id = showing['id'].split('_').last.to_i   tags = showing.css('.tags a').map { |tag| tag.text.strip }   title_el = showing.at_css('h1 a')   title_el.children.each { |c| c.remove if c.name == 'span' }   title = title_el.text.strip   dates = showing.at_css('.start_and_pricing').inner_html.strip   dates = dates.split('&lt;br&gt;').map(&:strip).map { |d| DateTime.parse(d) }   description = showing.at_css('.copy').text.gsub('[more...]', '').strip   showings.push(     id: showing_id,     title: title,     tags: tags,     dates: dates,     description: description   ) end <\/code><\/pre>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c \u043a\u043e\u0434, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435.<\/p>\n<pre><code class=\"ruby\">showing_id = showing['id'].split('_').last.to_i <\/code><\/pre>\n<p>  \u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u044b \u0431\u0435\u0440\u0435\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 id, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u044e\u0431\u0435\u0437\u043d\u043e \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043a\u0430\u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442 html \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u0432 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0435. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 html, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435, <code>showing['id']<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u00abevent_7557\u00bb. \u041d\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044f <code>.split('_')<\/code> \u0438 \u0437\u0430\u0442\u0435\u043c \u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 <code>.last.to_i<\/code>.<\/p>\n<pre><code class=\"ruby\">tags = showing.css('.tags a').map { |tag| tag.text.strip } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0432\u0441\u0435 \u0442\u0435\u0433\u0438 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u043c\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>.css<\/code> \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u043c\u0430\u043f\u0438\u043c (\u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434 map) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0431\u0435\u0440\u0435\u043c \u0438\u0437 \u043d\u0438\u0445 \u0442\u0435\u043a\u0441\u0442 \u0438 \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0432 \u043d\u0435\u043c \u043f\u0440\u043e\u0431\u0435\u043b\u044b. \u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e html \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0443\u0434\u0435\u0442 <code>[&quot;comedy&quot;, &quot;dvd&quot;, &quot;film&quot;]<\/code>.<\/p>\n<pre><code class=\"ruby\">title_el = showing.at_css('h1 a') title_el.children.each { |c| c.remove if c.name == 'span' } title = title_el.text.strip <\/code><\/pre>\n<p>  \u041a\u043e\u0434 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 html \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u0431\u0430\u0432\u043e\u0447\u043d\u044b\u0435 span \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0430\u043c\u0438 \u0438 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u0430\u043c\u0438. \u041c\u044b \u0431\u0435\u0440\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>.at_css<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0434\u0438\u043d \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043c\u043a\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 span. \u0412 \u043a\u043e\u043d\u0446\u0435, \u043a\u043e\u0433\u0434\u0430 span \u0443\u0431\u0440\u0430\u043d\u044b \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0438 \u0447\u0438\u0441\u0442\u0438\u043c \u0435\u0433\u043e \u043e\u0442 \u043b\u0438\u0448\u043d\u0438\u0445 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432. <\/p>\n<pre><code class=\"ruby\">dates = showing.at_css('.start_and_pricing').inner_html.strip dates = dates.split('&lt;br&gt;').map(&:strip).map { |d| DateTime.parse(d) } <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u0442\u044b \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e\u043a\u0430\u0437\u0430. \u0417\u0434\u0435\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0444\u0438\u043b\u044c\u043c\u044b \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u0438, \u0438\u043d\u043e\u0433\u0434\u0430, \u0446\u0435\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432 \u044d\u0442\u043e\u043c \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0435. \u041c\u044b \u043c\u0430\u043f\u0438\u043c \u0434\u0430\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0439\u0434\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>DateTime.parse<\/code> \u0438 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 Ruby \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u2014 <code>DateTime<\/code>. <\/p>\n<pre><code class=\"ruby\">description = showing.at_css('.copy').text.gsub('[more...]', '').strip <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0447\u0442\u043e \u0441\u0442\u043e\u0438\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u0443\u0431\u0440\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 <code>[more...]<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>.gsub<\/code><\/p>\n<pre><code class=\"ruby\">showings.push(     id: showing_id,     title: title,     tags: tags,     dates: dates,     description: description   ) <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0438\u043c\u0435\u044f \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0445 \u0432 \u043d\u0430\u0448 \u0445\u0435\u0448 (hash), \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0444\u0438\u043b\u044c\u043c\u043e\u0432. <\/p>\n<h3>\u0412\u044b\u0432\u043e\u0434 \u0432 JSON<\/h3>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u043e\u0442\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0444\u0438\u043b\u044c\u043c \u0438 \u043c\u044b \u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u043e\u0436\u0435\u043c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 JSON. <\/p>\n<pre><code class=\"ruby\">require 'json'  puts JSON.pretty_generate(showings) <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 showings \u043f\u0435\u0440\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 JSON, \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432\u044b\u0432\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0444\u0430\u0439\u043b \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438. <\/p>\n<h3>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435<\/h3>\n<p>  \u0421\u043e\u0431\u0440\u0430\u0432 \u0432\u0441\u0435 \u0447\u0430\u0441\u0442\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u043b\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430:<\/p>\n<pre><code class=\"ruby\">require 'open-uri' require 'nokogiri' require 'json'  url = 'http:\/\/www.cubecinema.com\/programme' html = open(url)  doc = Nokogiri::HTML(html) showings = [] doc.css('.showing').each do |showing|   showing_id = showing['id'].split('_').last.to_i   tags = showing.css('.tags a').map { |tag| tag.text.strip }   title_el = showing.at_css('h1 a')   title_el.children.each { |c| c.remove if c.name == 'span' }   title = title_el.text.strip   dates = showing.at_css('.start_and_pricing').inner_html.strip   dates = dates.split('&lt;br&gt;').map(&:strip).map { |d| DateTime.parse(d) }   description = showing.at_css('.copy').text.gsub('[more...]', '').strip   showings.push(     id: showing_id,     title: title,     tags: tags,     dates: dates,     description: description   ) end  puts JSON.pretty_generate(showings) <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u044d\u0442\u043e \u0432 \u0444\u0430\u0439\u043b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>scraper.rb<\/code> \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>ruby scraper.rb<\/code>, \u0442\u043e \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON. \u041e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043d\u0438\u0436\u0435.<\/p>\n<pre><code>[   {     &quot;id&quot;: 7686,     &quot;title&quot;: &quot;Harry Dean Stanton - Partly Fiction&quot;,     &quot;tags&quot;: [       &quot;dcp&quot;,       &quot;film&quot;,       &quot;ttt&quot;     ],     &quot;dates&quot;: [       &quot;2015-01-19T20:00:00+00:00&quot;,       &quot;2015-01-20T20:00:00+00:00&quot;     ],     &quot;description&quot;: &quot;A mesmerizing, impressionistic portrait of the iconic actor in his intimate moments, with film clips from some of his 250 films and his own heart-breaking renditions of American folk songs. ...&quot;   },   {     &quot;id&quot;: 7519,     &quot;title&quot;: &quot;Bang the Bore Audiovisual Spectacle: VA AA LR + Stephen Cornford + Seth Cooke&quot;,     &quot;tags&quot;: [       &quot;music&quot;     ],     &quot;dates&quot;: [       &quot;2015-01-21T20:00:00+00:00&quot;     ],     &quot;description&quot;: &quot;An evening of hacked TVs, 4 screen cinematic drone and electroacoustics. VAAALR: Vasco Alves, Adam Asnan and Louie Rice create spectacles using distress flares, C02 and junk electronics. Stephen Cornford: ...&quot;   } ] <\/code><\/pre>\n<p>  \u0412\u0441\u0451. \u0418 \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430. \u0421\u043b\u043e\u0436\u043d\u0435\u0435 \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0441\u0430\u0439\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u0414\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 <a href=\"http:\/\/docs.seattlerb.org\/mechanize\/GUIDE_rdoc.html\">mechanize<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u0434 Nokogiri.<\/p>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0434\u0430\u043d\u043d\u043e\u0435 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0434\u0430\u0441\u0442 \u0432\u0430\u043c \u0438\u0434\u0435\u0438 \u043e \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c \u0432 \u0431\u043e\u043b\u0435\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435. <\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u044e \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 &lt;a href=\u00ab<a href=\"https:\/\/www.chrismytton.uk\/2015\/01\/22\/advanced-web-scraping-with-mechanize\/\">www.chrismytton.uk\/2015\/01\/22\/advanced-web-scraping-with-mechanize\/<\/a>\u00bb target=&quot;_blank&gt;\u0434\u0440\u0443\u0433\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u043d\u0430 \u0442\u0435\u043c\u0443 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0436\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/p>\n<p>  1: Open-uri \u0445\u043e\u0440\u043e\u0448 \u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0432\u0435\u0449\u0435\u0439, \u0432\u0440\u043e\u0434\u0435 \u0442\u0435\u0445, \u0447\u0442\u043e \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0432 \u0443\u0440\u043e\u043a\u0435, \u043d\u043e \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 <a href=\"https:\/\/bugs.ruby-lang.org\/issues\/3719\">\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/a>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u0434\u0440\u0443\u0433\u043e\u0439 http \u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d \u0441\u0440\u0435\u0434\u044b. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/252379\/\"> http:\/\/habrahabr.ru\/post\/252379\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p> \t\t\t<i>\u042d\u0442\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/www.chrismytton.uk\/2015\/01\/19\/web-scraping-with-ruby\/\">\u00abWeb Scraping with Ruby\u00bb<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u043d\u0430\u0448\u0435\u043b \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u043f\u0440\u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0438 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Ruby. \u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u043c\u0435\u043d\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u0432 \u043b\u0438\u0447\u043d\u044b\u0445 \u0446\u0435\u043b\u044f\u0445. \u041c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u044d\u0442\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043d\u0430\u0432\u044b\u043a, \u043d\u043e \u0438 \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u044f\u0437\u044b\u043a.<\/i>   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-252583","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/252583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=252583"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/252583\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}