English 中文(简体)
为什么我每个桌上都有一个空白记录?
原标题:Why do I get a blank record for every table row?

我的法典如下:

page = agent.page.search("table tbody tr").each do |row|
  time        = row.css("td:nth-child(1)").text.strip
  source      = row.css("td:nth-child(2)").text.strip
  destination = row.css("td:nth-child(3)").text.strip
  duration    = row.css("td:nth-child(4)").text.strip
  Call.create!(:time => time, :source => source, :destination => destination, :duration => duration)
end

在我执行ake忙任务时,它正确地将数据放在我铁路应用中的正确表格中,但出于某种原因,在成功地为一行树立了记录,同时也造成了空白记录。

我可以这样说。 从该法典的角度来看,它签发了<代码>create!。 每一行指挥。

You can see the full rake task at https://gist.github.com/1574942 and the other question leading to this code is "Parse html into Rails without new record every time?".

最佳回答

根据该评论:

我认为,你是正确的,我对边远网页的超文本进行了研究,并且正在每台桌上增加一个分级的总结。 我不禁要问,是否用任何手段来绕过空行?

如果你再次看到像:

<table>
  <tbody>
    <tr>
      <tr>
        <td>time</td>
        <td>source</td>
        <td>destination</td>
        <td>duration</td>
      </tr>
    </tr>
  </tbody>
</table>

然后,这将表明问题:

require  nokogiri 
require  pp 

html =  <table><tbody><tr><tr><td>time</td><td>source</td><td>destination</td><td>duration</td></tr></tr></tbody></table> 
doc = Nokogiri::HTML(html)
page = doc.search("table tbody tr").each do |row|
  time        = row.css("td:nth-child(1)").text.strip
  source      = row.css("td:nth-child(2)").text.strip
  destination = row.css("td:nth-child(3)").text.strip
  duration    = row.css("td:nth-child(4)").text.strip
  hash = {
    :time        => time,
    :source      => source,
    :destination => destination,
    :duration    => duration 
  }
  pp hash
end

这些产出:

{:time=>"", :source=>"", :destination=>"", :duration=>""}
{:time=>"time",
 :source=>"source",
 :destination=>"destination",
 :duration=>"duration"}

由于超文本不明确,你之所以获得空白浏览。 <代码><tr> ben t. 固定装置是容易的,并将使用同样正确的超文本。

Also, the inner css access is not quite correct, but why that is so is subtle. I ll get to that.

为了确定第一种标准,我们要补充一个有条件的检验标准:

page = doc.search("table tbody tr").each do |row|

成为:

page = doc.search("table tbody tr").each do |row|
  next if (!row.at( td ))

产出在运行后:

{:time=>"time",
 :source=>"source",
 :destination=>"destination",
 :duration=>"duration"}

这真是你们都需要解决这一问题,但法典中有一些事情要做,很难做一些解释,但首先需要修改法典:

来源:

time        = row.css("td:nth-child(1)").text.strip
source      = row.css("td:nth-child(2)").text.strip
destination = row.css("td:nth-child(3)").text.strip
duration    = row.css("td:nth-child(4)").text.strip

改为:

time, source, destination, duration = row.search( td ).map{ |td| td.text.strip }

坚持该守则对你想要的产出:

{:time=>"time",
 :source=>"source",
 :destination=>"destination",
 :duration=>"duration"}

因此,这些事情还剩下 h。

这个问题与你原来的法典有关:

<代码>cs为search的正文。 Nokogiri将双面的诺德Set归还text。 查阅外部<代码><tr>。 因此,Nokogiri没有 silent声地做你想要做的事情,因为鉴于你告诉它做些什么,这是完全正确和合乎逻辑的;这只是没有达到你的期望。

使用<代码>at,或如cs_at,查询第一个配对器。 因此,理论上,我们可以继续使用<代码>row.at(td:nth-child(1)”)的案文。strip,为每位准入者安排多项任务,这将立即暴露出你与超文本存在问题,因为<>text将受到打击。 但这还不够。

相反,我们可以利用<>地图在NodeSet返回的囚室,让它收集所需的囚室内容并加以脱光,然后对变数进行平行分配:

time, source, destination, duration = row.search( td ).map{ |td| td.text.strip }

同样,继续这样做:

require  nokogiri 
require  pp 

html =  <table><tbody><tr><tr><td>time</td><td>source</td><td>destination</td><td>duration</td></tr></tr></tbody></table> 
doc = Nokogiri::HTML(html)
page = doc.search("table tbody tr").each do |row|
  next if (!row.at( td ))

  time, source, destination, duration = row.search( td ).map{ |td| td.text.strip }

  hash = {
    :time        => time,
    :source      => source,
    :destination => destination,
    :duration    => duration 
  }
  pp hash
end

我:

{:time=>"time",
 :source=>"source",
 :destination=>"destination",
 :duration=>"duration"}

符合你的法典,并符合:

page = agent.page.search("table tbody tr").each do |row|
  next if (!row.at( td ))
  time, source, destination, duration = row.search( td ).map{ |td| td.text.strip }
  Call.create!(:time => time, :source => source, :destination => destination, :duration => duration)
end

或许不需要<代码>网页=。

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签