Search string in HTML file

Discussion in 'Ruby' started by Jean Verger, Sep 22, 2006.

  1. Jean Verger

    Jean Verger Guest

    Hi,
    I'm looking the way of searching an string in a HTML file given the URL.

    The parameter will be "<span>value</span>" ... it has to be that
    format, exactly.

    I'm trying with Hpricot (http://code.whytheluckystiff.net/hpricot)
    which is very powerfull ... but it searches for elements, so i only
    can pass the value of the SPAN .. no the whole string.

    anyway of searching for an string in a html file given the URL?

    thanks,

    Jean
     
    Jean Verger, Sep 22, 2006
    #1
    1. Advertising

  2. Jean Verger

    Jan Svitok Guest

    On 9/22/06, Jean Verger <> wrote:
    > Hi,
    > I'm looking the way of searching an string in a HTML file given the URL.
    >
    > The parameter will be "<span>value</span>" ... it has to be that
    > format, exactly.
    >
    > I'm trying with Hpricot (http://code.whytheluckystiff.net/hpricot)
    > which is very powerfull ... but it searches for elements, so i only
    > can pass the value of the SPAN .. no the whole string.
    >
    > anyway of searching for an string in a html file given the URL?
    >
    > thanks,
    >
    > Jean


    Will this do it? (I have not run it, so there may be errors.)

    require 'open-uri'

    uri = URI.parse('http://whatever')
    data = uri.read
    data.scan('<sub>whatever</sub>')
     
    Jan Svitok, Sep 22, 2006
    #2
    1. Advertising

  3. Hi Jean,

    Here is my version of it:

    require 'open-uri'

    # this returns true if the string is found
    open('http://whatever').find { |line| line.match('<span>value</span>') }
    != nil

    --
    Posted via http://www.ruby-forum.com/.
     
    Stephane Elie, Sep 22, 2006
    #3
  4. Jean Verger

    Jean Verger Guest

    thanks ... simple and efficient :) it works

    thanks,

    Jean

    On 9/22/06, Stephane Elie <> wrote:
    > Hi Jean,
    >
    > Here is my version of it:
    >
    > require 'open-uri'
    >
    > # this returns true if the string is found
    > open('http://whatever').find { |line| line.match('<span>value</span>') }
    > != nil
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Jean Verger, Sep 26, 2006
    #4
  5. Jean Verger

    Madu Nar Guest

    Jean Verger wrote:
    > thanks ... simple and efficient :) it works
    >
    > thanks,
    >
    > Jean


    Hello there,
    I am a newbie to Ruby and Watir.
    Currently, me trying to extract a parameter from the html page. But the
    Value I am trying to extract keeps changing and depends on the input.
    Here is the piece of code I tried which did not work...

    require 'open-uri'
    require'watir'
    require 'fileutils'
    require 'net/http'

    d_term = "Banks"

    url1 = Whatever...
    MT = 'Search '+'<b>'+d_term.chomp+'</b>'
    open(url1).find {|line|
    if line.match(MT)
    foo = File.open("c://abc.txt", "a+")
    foo.puts ("Found: " + line)
    foo.close()
    end

    Any help greatly appreciated.

    Thanks,
    Madu

    --
    Posted via http://www.ruby-forum.com/.
     
    Madu Nar, Sep 28, 2006
    #5
  6. Jean Verger

    Jean Verger Guest

    mmm something like this?

    url1 = Whatever...
    MT = 'Search '+'<b>'+d_term.chomp+'</b>'
    if (open(url1).find { |line| line.match(MT) } != nil)
    foo = File.open("c://abc.txt", "a+")
    foo.puts ("Found: " + line)
    foo.close()
    end

    On 9/28/06, Madu Nar <> wrote:
    > Jean Verger wrote:
    > > thanks ... simple and efficient :) it works
    > >
    > > thanks,
    > >
    > > Jean

    >
    > Hello there,
    > I am a newbie to Ruby and Watir.
    > Currently, me trying to extract a parameter from the html page. But the
    > Value I am trying to extract keeps changing and depends on the input.
    > Here is the piece of code I tried which did not work...
    >
    > require 'open-uri'
    > require'watir'
    > require 'fileutils'
    > require 'net/http'
    >
    > d_term = "Banks"
    >
    > url1 = Whatever...
    > MT = 'Search '+'<b>'+d_term.chomp+'</b>'
    > open(url1).find {|line|
    > if line.match(MT)
    > foo = File.open("c://abc.txt", "a+")
    > foo.puts ("Found: " + line)
    > foo.close()
    > end
    >
    > Any help greatly appreciated.
    >
    > Thanks,
    > Madu
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Jean Verger, Sep 28, 2006
    #6
  7. Jean Verger

    Madu Nar Guest

    Jean Verger wrote:
    > mmm something like this?
    >
    > url1 = Whatever...
    > MT = 'Search '+'<b>'+d_term.chomp+'</b>'
    > if (open(url1).find { |line| line.match(MT) } != nil)
    > foo = File.open("c://abc.txt", "a+")
    > foo.puts ("Found: " + line)
    > foo.close()
    > end




    Hello Jean,

    On execution the program gives me the following errors...

    frame error in wait document
    OLE error code:8007005 in <unknown>
    Access is denied

    HRESULT error code:0x8002009
    Exception Occured

    Me not sure how to get rid of this. It works till opening the requested
    page but finding and writing part does not seem to work...
    any more suggestions?

    Thanks again,
    Madu

    --
    Posted via http://www.ruby-forum.com/.
     
    Madu Nar, Sep 28, 2006
    #7
  8. Jean Verger

    Jean Verger Guest

    dont know those errors ..., you can troubleshoot ...
    make sure that "open(url1)" it is not givin you an error .. If that
    line is giving you an error, it means the web site doesn't exist or
    you are including the open-uri code ( include 'open-uri.rb (i believe
    is the line) :)

    regards,

    Jean

    On 9/28/06, Madu Nar <> wrote:
    > Jean Verger wrote:
    > > mmm something like this?
    > >
    > > url1 = Whatever...
    > > MT = 'Search '+'<b>'+d_term.chomp+'</b>'
    > > if (open(url1).find { |line| line.match(MT) } != nil)
    > > foo = File.open("c://abc.txt", "a+")
    > > foo.puts ("Found: " + line)
    > > foo.close()
    > > end

    >
    >
    >
    > Hello Jean,
    >
    > On execution the program gives me the following errors...
    >
    > frame error in wait document
    > OLE error code:8007005 in <unknown>
    > Access is denied
    >
    > HRESULT error code:0x8002009
    > Exception Occured
    >
    > Me not sure how to get rid of this. It works till opening the requested
    > page but finding and writing part does not seem to work...
    > any more suggestions?
    >
    > Thanks again,
    > Madu
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Jean Verger, Sep 29, 2006
    #8

  9. > require 'open-uri'
    > require'watir'
    > require 'fileutils'
    > require 'net/http'


    Madu,

    The error you have is coming out of Watir which unfortunately I have no
    experience with.

    For the code sample you posted, you only need to include open-uri.

    watir, fileutils and net/http are unused by your code sample.

    As Jean proposed, I suggest you debug your application by breaking it in
    smaller pieces.

    Stephane

    --
    Posted via http://www.ruby-forum.com/.
     
    Stephane Elie, Sep 29, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SklNLkgu?=

    Q: search string in a string

    =?Utf-8?B?SklNLkgu?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    346
    =?Utf-8?B?SklNLkgu?=
    Mar 21, 2005
  2. Bengt Richter
    Replies:
    0
    Views:
    528
    Bengt Richter
    Aug 3, 2003
  3. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    607
    Anand Pillai
    Oct 15, 2003
  4. Abby Lee
    Replies:
    5
    Views:
    443
    Abby Lee
    Aug 2, 2004
  5. Replies:
    1
    Views:
    199
    Dr.Ruud
    Jul 29, 2006
Loading...

Share This Page