【问题标题】:Webscraping - Extracting individual items from a column that contains multiple items (Nokogiri, Ruby)Webscraping - 从包含多个项目的列中提取单个项目(Nokogiri,Ruby)
【发布时间】:2018-01-05 23:53:19
【问题描述】:

我是网络开发的新手,正在尝试学习网络抓取。我试图从中提取的列包含多个项目,我想分别获取各个项目。

site = "http://www.cyrptomarketcap.com"
doc = Nokogiri::HTML.parse(open(site))
coin_table = doc.css("table").sort { |x,y| y.css("tr").count <=> x.css("tr").count }.first
rows = coin_table.cc("tr")
rows = rows.select { |row| row.css("th").empty? }
data = rows.map do |row|
  [row.at_css("td:nth-child(2)").try(:text)]
end

这样的结果是:

=> [["\n \n \n \n BTC\n Bitcoin\n "], ["\n \n \n \n XRP\n Ripple\n "], ... ]

我需要做什么才能只得到名字?

【问题讨论】:

  • 你所说的“只是名字”是什么意思?
  • [比特币,瑞波币,...]
  • 成功了。谢谢。

标签: ruby-on-rails ruby web-scraping nokogiri web-development-server


【解决方案1】:

您可以将以下内容应用于您的 data 变量:

data.flatten.map { |s| s.split("\n")[-2].gsub(' ', '') }

它给出了结果:

["Bitcoin", "Ripple", ...]

【讨论】:

    猜你喜欢
    • 2022-07-22
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2019-07-01
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多