Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析不在html标记Nokogiri中的内容_Html_Ruby_Screen Scraping_Nokogiri - Fatal编程技术网

解析不在html标记Nokogiri中的内容

解析不在html标记Nokogiri中的内容,html,ruby,screen-scraping,nokogiri,Html,Ruby,Screen Scraping,Nokogiri,有了这个,我只能得到电影的标题,一个电影院和两个联系电话,所以我的示例输出看起来像这样 require 'rubygems' require 'nokogiri' require 'open-uri' url = "http://flix.co.ke/Frontpage/Listings" doc = Nokogiri::HTML(open(url)) doc.css(".min-width div form").each do |entry| title = entry.at_cs

有了这个,我只能得到电影的
标题
一个电影院
两个联系电话
,所以我的示例输出看起来像这样

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "http://flix.co.ke/Frontpage/Listings"
doc = Nokogiri::HTML(open(url))

doc.css(".min-width div form").each do |entry|
    title = entry.at_css("span").text
    puts title

    cinema = entry.at_css("br+ strong").text
    puts cinema

    phone = entry.at_css("a").text
    puts phone

    puts entry.at_css("a").next_element.text
end
标题后面有一个描述,就在break标签之后,我无法得到它,我如何在
标签内循环浏览所有电影院?还有以逗号分隔的电话号码和个人演出时间

我只是不知道从哪里开始。我希望在这个案件中取得这样的结果

  • 12年的奴隶生活

  • 在战前的美国,来自纽约州北部的自由黑人所罗门·诺瑟普被绑架并被贩卖为奴隶

  • 世纪X交叉路口 0774136246 0208022073 12:10,19:10,21:40
  • 福克斯影城纱丽 0203753025 0720366208 11:00、14:00、18:00、20:40


我们将非常感谢您的帮助。提前感谢

这是可怕的HTML:/它无效,有451个错误和9个警告。没有语义,所以你必须依赖于结构,它可能会改变,打破你的刮

尽管如此,您仍可以通过使用同级方法获得其中的每一项:

12 Years a Slave
Century Cinemax - Junction
0774136246
0208022073

47 Ronin 3D
Century Cinemax - Junction
0774136246
0208022073

Delivery Man
Century Cinemax - Junction
0774136246
0208022073

Frozen
Century Cinemax - Junction
0774136246
0208022073

(continued...)

您可以使用
css
而不是
at_css
(例如,与您循环表单元素的方式相同)

html真的没有那么糟糕,而且您使用
br+strong
的方法是正确的,这就是您要迭代的内容:

doc.css('.min-width div form').each do |node|
  description = node.at_css('br').next_sibling.text
  puts description.strip
  puts '-'*10
end

# >> In the antebellum United States, Solomon Northup, a free black man from upstate New York, is abducted and sold into slavery.
# >> ----------
# >> A band of samurai set out to avenge the death and dishonor of their master at the hands of a ruthless shogun.
# >> ----------
# >> An affable underachiever finds out he's fathered 533 children through anonymous donations to a fertility clinic 20 years ago. Now he must decide whether or not to come forward when 142 of them file a lawsuit to reveal his identity.
# >> ----------
# >> Fearless optimist Anna teams up with Kristoff in an epic journey, encountering Everest-like conditions, and a hilarious snowman named Olaf in a race to find Anna's sister Elsa, whose icy powers have trapped the kingdom in eternal winter.
# >> ----------
# >> A medical engineer and an astronaut work together to survive after an accident leaves them adrift in space.
# >> ----------
# >> A pair of aging boxing rivals are coaxed out of retirement to fight one final bout -- 30 years after their last match.
# >> ----------
# >> 
# >> ----------
# >> Harrison, overworked and underpaid is looking for money for bride price. A 'business' opportunity presents itself when he gets the keys to the Company house. With the CEO away on holiday, he has access to a vacant fully furnished house. He ...
# >> ----------
# >> 
# >> ----------
# >> A chronicle of Nelson Mandela's life journey from his childhood in a rural village through to his inauguration as the first democratically elected president of South Africa.
# >> ----------
# >> Author P. L. Travers reflects on her difficult childhood while meeting with filmmaker Walt Disney during production for the adaptation of her novel, Mary Poppins.
# >> ----------
# >> The Manzoni family, a notorious mafia clan, is relocated to Normandy, France under the witness protection program, where fitting in soon becomes challenging as their old habits die hard.
# >> ----------
# >> The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.
# >> ----------
# >> The film begins as Katniss Everdeen has returned home safe after winning the 74th Annual Hunger Games along with fellow tribute Peeta Mellark. Winning means that they must turn around and leave their family and close friends, embarking on a ...
# >> ----------
# >> A day-dreamer escapes his anonymous life by disappearing into a world of fantasies filled with heroism, romance and action. When his job along with that of his co-worker are threatened, he takes action in the real world embarking on a global ...
# >> ----------
# >> Faced with an enemy that even Odin and Asgard cannot withstand, Thor must embark on his most perilous and personal journey yet, one that will reunite him with Jane Foster and force him to sacrifice everything to save us all.
# >> ----------
# >> A journey into the lives of a mother polar bear and her two seven-month-old cubs as they navigate the changing Arctic wilderness they call home.
# >> ----------
# >> See and feel what it was like when dinosaurs ruled the Earth, in a story where an underdog dino triumphs to become a hero for the ages.
# >> ----------
doc.search('.min-width div form')。每个do|form|
title=form.at('span')。text
description=form.at('br')。next.text
form.search('br+strong')。每个都可以|
电影院=el.text
电话=[]
而next_el=el.at('+a','+br+a'))
el=下一个

手机包含有效的HTML代码段,而不是摘录。为了帮助你,我们必须跳出障碍。更好的方法!我不能强调这有多大帮助。非常感谢!;-)
doc.css('.min-width div form').each do |node|
  description = node.at_css('br').next_sibling.text
  puts description.strip
  puts '-'*10
end

# >> In the antebellum United States, Solomon Northup, a free black man from upstate New York, is abducted and sold into slavery.
# >> ----------
# >> A band of samurai set out to avenge the death and dishonor of their master at the hands of a ruthless shogun.
# >> ----------
# >> An affable underachiever finds out he's fathered 533 children through anonymous donations to a fertility clinic 20 years ago. Now he must decide whether or not to come forward when 142 of them file a lawsuit to reveal his identity.
# >> ----------
# >> Fearless optimist Anna teams up with Kristoff in an epic journey, encountering Everest-like conditions, and a hilarious snowman named Olaf in a race to find Anna's sister Elsa, whose icy powers have trapped the kingdom in eternal winter.
# >> ----------
# >> A medical engineer and an astronaut work together to survive after an accident leaves them adrift in space.
# >> ----------
# >> A pair of aging boxing rivals are coaxed out of retirement to fight one final bout -- 30 years after their last match.
# >> ----------
# >> 
# >> ----------
# >> Harrison, overworked and underpaid is looking for money for bride price. A 'business' opportunity presents itself when he gets the keys to the Company house. With the CEO away on holiday, he has access to a vacant fully furnished house. He ...
# >> ----------
# >> 
# >> ----------
# >> A chronicle of Nelson Mandela's life journey from his childhood in a rural village through to his inauguration as the first democratically elected president of South Africa.
# >> ----------
# >> Author P. L. Travers reflects on her difficult childhood while meeting with filmmaker Walt Disney during production for the adaptation of her novel, Mary Poppins.
# >> ----------
# >> The Manzoni family, a notorious mafia clan, is relocated to Normandy, France under the witness protection program, where fitting in soon becomes challenging as their old habits die hard.
# >> ----------
# >> The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.
# >> ----------
# >> The film begins as Katniss Everdeen has returned home safe after winning the 74th Annual Hunger Games along with fellow tribute Peeta Mellark. Winning means that they must turn around and leave their family and close friends, embarking on a ...
# >> ----------
# >> A day-dreamer escapes his anonymous life by disappearing into a world of fantasies filled with heroism, romance and action. When his job along with that of his co-worker are threatened, he takes action in the real world embarking on a global ...
# >> ----------
# >> Faced with an enemy that even Odin and Asgard cannot withstand, Thor must embark on his most perilous and personal journey yet, one that will reunite him with Jane Foster and force him to sacrifice everything to save us all.
# >> ----------
# >> A journey into the lives of a mother polar bear and her two seven-month-old cubs as they navigate the changing Arctic wilderness they call home.
# >> ----------
# >> See and feel what it was like when dinosaurs ruled the Earth, in a story where an underdog dino triumphs to become a hero for the ages.
# >> ----------
doc.search('.min-width div form').each do |form|
  title = form.at('span').text
  description = form.at('br').next.text

  form.search('br + strong').each do |el|
    cinema = el.text
    phones = []
    while next_el = el.at('+ a', '+ br + a')
      el = next_el
      phones << el.text
    end
    times = el.at('+ br').next.text        
  end
end