Issues with Time.parse

J

Jens Finnäs

Hi,

I just started working my way around web scraping with Ruby and I just
run in to some problems with Time.parse.

I'm scraping a date from a website:

collect_last_bid =
last_bid_cell.scan(/\d{1,2}\.\d{1,2}\.\d{1,4}\b.\b\d{1,2}\:\d{1,2}:\d{1,2}/)

...which comes out nicely as "26.01.2011 21:00:08"

However, when I Time.parse the variable:

right_now = Time.parse(collect_last_bid)

...I get "Error in time.rb, Line 240 in 'parse'"

Parsing the string with Time.parse("26.01.2011 21:00:08") works fine.

Any thoughts?
 
J

Jesús Gabriel y Galán

Hi,

I just started working my way around web scraping with Ruby and I =A0just
run in to some problems with Time.parse.

I'm scraping a date from a website:

collect_last_bid =3D
last_bid_cell.scan(/\d{1,2}\.\d{1,2}\.\d{1,4}\b.\b\d{1,2}\:\d{1,2}:\d{1,2= }/)

...which comes out nicely as "26.01.2011 21:00:08"

However, when I Time.parse the variable:

right_now =3D Time.parse(collect_last_bid)

...I get "Error in time.rb, Line 240 in 'parse'"

Parsing the string with Time.parse("26.01.2011 21:00:08") works fine.

Any thoughts?

String#scan returns an array:

irb(main):001:0> s =3D "<td>26.01.2011 21:00:08</td>"
=3D> "<td>26.01.2011 21:00:08</td>"
irb(main):003:0> time =3D
s.scan(/\d{1,2}\.\d{1,2}\.\d{1,4}\b.\b\d{1,2}\:\d{1,2}:\d{1,2}/)
=3D> ["26.01.2011 21:00:08"]
irb(main):005:0> require 'time'
=3D> true
irb(main):006:0> Time.parse(time)
NoMethodError: private method `gsub!' called for ["26.01.2011 21:00:08"]:Ar=
ray
from /usr/lib/ruby/1.8/date/format.rb:1061:in `_parse'
from /usr/lib/ruby/1.8/time.rb:240:in `parse'
from (irb):6
from =03:0
irb(main):007:0> Time.parse(time[0])
=3D> Wed Jan 26 21:00:08 +0100 2011

Jesus.
 
R

Robert Klemme

2011/1/26 Jes=FAs Gabriel y Gal=E1n said:
Hi,

I just started working my way around web scraping with Ruby and I =A0jus= t
run in to some problems with Time.parse.

I'm scraping a date from a website:

collect_last_bid =3D
last_bid_cell.scan(/\d{1,2}\.\d{1,2}\.\d{1,4}\b.\b\d{1,2}\:\d{1,2}:\d{1,= 2}/)

...which comes out nicely as "26.01.2011 21:00:08"

However, when I Time.parse the variable:

right_now =3D Time.parse(collect_last_bid)

...I get "Error in time.rb, Line 240 in 'parse'"

Parsing the string with Time.parse("26.01.2011 21:00:08") works fine.

Any thoughts?

String#scan returns an array:

irb(main):001:0> s =3D "<td>26.01.2011 21:00:08</td>"
=3D> "<td>26.01.2011 21:00:08</td>"
irb(main):003:0> time =3D
s.scan(/\d{1,2}\.\d{1,2}\.\d{1,4}\b.\b\d{1,2}\:\d{1,2}:\d{1,2}/)
=3D> ["26.01.2011 21:00:08"]
irb(main):005:0> require 'time'
=3D> true
irb(main):006:0> Time.parse(time)
NoMethodError: private method `gsub!' called for ["26.01.2011 21:00:08"]:= Array
=A0 =A0 =A0 =A0from /usr/lib/ruby/1.8/date/format.rb:1061:in `_parse'
=A0 =A0 =A0 =A0from /usr/lib/ruby/1.8/time.rb:240:in `parse'
=A0 =A0 =A0 =A0from (irb):6
=A0 =A0 =A0 =A0from =A0:0
irb(main):007:0> Time.parse(time[0])
=3D> Wed Jan 26 21:00:08 +0100 2011

When parsing the input manually then Time.local or Time.utc are better IMHO=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top