[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to get certain links that contains some string

PP

5/15/2006 3:34:00 AM

Now I have a problem. What I want is some urls in a certain table of a
web page. How could I get these URLs and save them as an array so as to
visit the web pages that these URLs navigate to.
My codes as follows:
##
require'Watir'
ie=Watir::IE.new
ie.goto("www.baidu.com")
n=ie.links.length
puts n
i=1
j=1
a1=Array.new #a1 is used to store all the links in the
page
a2=Array.new #a2 is used to store the certain links
that contains the string 'baidu'
while i<n
a1[i]=ie.links[i].to_s
if a1[i].matches(/.*baidu.*/)
puts a1[i]
a2[j]=a1[i]
else puts "not found"
j=j+1
end
i=i+1
end
##
The result shows "not found" n times
Can anyone help me ?

2 Answers

Chris Hulan

5/15/2006 3:00:00 PM

0

try changing the 'matches' line to:
if /baidu/.matches(al[i])

I believe the ',*' are unnecessary. You may want to add the flag to
ignore case on the RegExp match.

FYI
Your code uses the STRING#matches defined by WAITR, which is
implemented as '==' (acording to the API RDocs) which is an exact
match. WAITR implments the RegExp*matches as a call to RegExp#match
which will return the match if found, or nil if not found.

Cheers

PP

5/15/2006 4:36:00 PM

0

Thank you for your advice. Your method really worked.
Best wishes!
ChrisH ??:

> try changing the 'matches' line to:
> if /baidu/.matches(al[i])
>
> I believe the ',*' are unnecessary. You may want to add the flag to
> ignore case on the RegExp match.
>
> FYI
> Your code uses the STRING#matches defined by WAITR, which is
> implemented as '==' (acording to the API RDocs) which is an exact
> match. WAITR implments the RegExp*matches as a call to RegExp#match
> which will return the match if found, or nil if not found.
>
> Cheers