我在页面上有一篇文章,我需要解析所有文本。
我知道一篇文章超过15个单词,加上符号''或','或' - ',或':'或'。'。
如何用Ruby编写Regexp来分析页面上的文章并解析它?
我需要解析正文:ATLANTA — From the sublime emotional high provided by Matt Harvey and Zack Wheeler, the Mets’ young, hard-throwing right-handers, the team on Wednesday descended back to the realities of its everyday existence...
我知道如何解析和获取页面内容,但我不知道如何在Regexp上编写它! 要分析带有所需文本的父HTML标记,我必须编写一些Regexp来检查规则: 文章超过15个单词,只加上符号''或','或' - ',或':'或'。'。
答案 0 :(得分:1)
看Nokogiri
以满足您的需求。这是网页报废的绝佳宝石。
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.nytimes.com/2013/06/20/sports/baseball/for-the-mets-an-afterglow-then-realitys-harsh-light.html?ref=sports&_r=1&'))
str = doc.at_css('div.articleBody > nyt_text > p').text
puts str
# >> ATLANTA — From the sublime emotional high provided by Matt Harvey and Zack Wheeler, the Mets’ young, hard-throwing right-handers, the team on Wednesday descended back to the realities of its everyday existence.
str.scan(/\w+/)
# => ["ATLANTA",
# "From",
# "the",
# "sublime",
# "emotional",
# "high",
# "provided",
# "by",
# "Matt",
# "Harvey",
# "and",
# "Zack",
# "Wheeler",
# "the",
# "Mets",
# "young",
# "hard",
# "throwing",
# "right",
# "handers",
# "the",
# "team",
# "on",
# "Wednesday",
# "descended",
# "back",
# "to",
# "the",
# "realities",
# "of",
# "its",
# "everyday",
# "existence"]
我知道该文章超过15个字词:
str.scan(/\w+/).size > 15 # => true
加入符号''或','或' - ',或':'或'。':
[' ',',','-',':','.'].map{|i| str.include? i}
# => [true, true, true, false, false]