How to get certain links that contains some string

P

PP

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=ie.links.to_s
if a1.matches(/.*baidu.*/)
puts a1
a2[j]=a1
else puts "not found"
j=j+1
end
i=i+1
end
##
The result shows "not found" n times
Can anyone help me ?
 
C

ChrisH

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

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
 
P

PP

Thank you for your advice. Your method really worked.
Best wishes!
ChrisH 写é“:
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top