converting the string

V

Vlad Smith

Hi everybody! I`m sorry for asking a silly question .. i hope you`ll
find some time to assist

while searching shrough html body i get the string :
<td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF'
width=624 height=280 border=0 align=absmiddle
usemap=#imIY_6NvJ_kj7s></td>

how can i modify it to get just a plain link without tags and params? :
https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF
 
G

Greg Willits

Vlad said:
Hi everybody! I`m sorry for asking a silly question .. i hope you`ll
find some time to assist

while searching shrough html body i get the string :
<td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF'
width=624 height=280 border=0 align=absmiddle
usemap=#imIY_6NvJ_kj7s></td>

how can i modify it to get just a plain link without tags and params? :
https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF

You'll have to expand this to take care of all possible scenarios, but
here's an example:

x = "<img src=\"http://some_url_to_scrape\">"
y = x.scan(/src=\"([\S\s]+?)\"/)

That will return an array, and you'll have to fish the string out of the
array with y[0][0].

That works specifically with proper HTML using double quotes where your
examples above used malformed single quotes, but you can either use
multple expressions, or build a more complex one to cover the various
cases of quotes href, src, and other attributes names, etc.

There's other ways to do it, this is just a small example to give you
some ideas.

-- gw
 
V

Vlad Smith

Greg said:
Vlad said:
Hi everybody! I`m sorry for asking a silly question .. i hope you`ll
find some time to assist

while searching shrough html body i get the string :
<td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF'
width=624 height=280 border=0 align=absmiddle
usemap=#imIY_6NvJ_kj7s></td>

how can i modify it to get just a plain link without tags and params? :
https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF

You'll have to expand this to take care of all possible scenarios, but
here's an example:

x = "<img src=\"http://some_url_to_scrape\">"
y = x.scan(/src=\"([\S\s]+?)\"/)

That will return an array, and you'll have to fish the string out of the
array with y[0][0].

That works specifically with proper HTML using double quotes where your
examples above used malformed single quotes, but you can either use
multple expressions, or build a more complex one to cover the various
cases of quotes href, src, and other attributes names, etc.

There's other ways to do it, this is just a small example to give you
some ideas.

-- gw

Thanks! that worked!

i also accidently noticed a great feature taken from perl that worked
also:

x = <td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s
s=www461.sj2&type=GIF'width=624 height=280 border=0
align=absmiddleusemap=#imIY_6NvJ_kj7s></td>
x = $1 if x =~ /.*(https.*GIF).*/
 
A

Aaron Patterson

Greg said:
Vlad said:
Hi everybody! I`m sorry for asking a silly question .. i hope you`ll
find some time to assist

while searching shrough html body i get the string :
<td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF'
width=624 height=280 border=0 align=absmiddle
usemap=#imIY_6NvJ_kj7s></td>

how can i modify it to get just a plain link without tags and params? :
https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF

You'll have to expand this to take care of all possible scenarios, but
here's an example:

x = "<img src=\"http://some_url_to_scrape\">"
y = x.scan(/src=\"([\S\s]+?)\"/)

That will return an array, and you'll have to fish the string out of the
array with y[0][0].

That works specifically with proper HTML using double quotes where your
examples above used malformed single quotes, but you can either use
multple expressions, or build a more complex one to cover the various
cases of quotes href, src, and other attributes names, etc.

There's other ways to do it, this is just a small example to give you
some ideas.

-- gw

Thanks! that worked!

i also accidently noticed a great feature taken from perl that worked
also:

x = <td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s
s=www461.sj2&type=GIF'width=624 height=280 border=0
align=absmiddleusemap=#imIY_6NvJ_kj7s></td>
x = $1 if x =~ /.*(https.*GIF).*/

Please don't do this. Every time you parse HTML with a regular
expression, a kitten dies.

Instead, try using an HTML parsing library:

x = <<-eohtml
<td class=blk11 ><img
src='https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7ss=www461.sj2&type=GIF'width=624 height=280 border=0
align=absmiddleusemap=#imIY_6NvJ_kj7s></td>
eohtml

puts Nokogiri::HTML(x).at('img')['src']
 

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

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top