0

I am working on 3rd Party application where I can only view to the Webpage source content.And from there I have to collect only some href content values which has pattern like /aems/file/filegetrevision.do?fileEntityId. Is it possible?

HTML *(Part of HTML)*

<td width="50%">
<a href="/aems/file/filegetrevision.do?fileEntityId=10597525&cs=9b7sjueBiWLBEMj2ZU4I6fyQoPv-g0NLY9ETqP0gWk4.xyz">
screenshot.doc
</a>
</td>
1
  • Check out Nokogiri. You could parse the document for only href that match the required pattern Jan 9, 2013 at 19:00

2 Answers 2

2

Easily:

require 'nokogiri'

html = '
<td width="50%">
<a href="/aems/file/filegetrevision.do?fileEntityId=10597525&cs=9b7sjueBiWLBEMj2ZU4I6fyQoPv-g0NLY9ETqP0gWk4.xyz">
screenshot.doc
</a>
</td>
'

doc = Nokogiri::HTML(html)
doc.search('a[href]').map{ |a| a['href'] }

Which returns:

[
    [0] "/aems/file/filegetrevision.do?fileEntityId=10597525&cs=9b7sjueBiWLBEMj2ZU4I6fyQoPv-g0NLY9ETqP0gWk4.xyz"
]

If you want to filter for path matches, use something like:

pattern = Regexp.escape('/aems/file/filegetrevision.do?fileEntityId')
doc.search('a[href]').map{ |a| a['href'] }.select{ |href| href[ %r[^#{ pattern }] ] }

Which, again, returns:

[
  [0] "/aems/file/filegetrevision.do?fileEntityId=10597525&cs=9b7sjueBiWLBEMj2ZU4I6fyQoPv-g0NLY9ETqP0gWk4.xyz"
]

This code will return the href parameter from all <a> tags with href in the document. In the second example, it will filter them by the path.

2
  • Here i just pasted part of an HTML but in current scenario,i have to pass URL onto the browser, and then from that webpage I have to collect such values of href. In such a case how should I frame your program? Jan 9, 2013 at 19:04
  • 1
    You need to write your question as you intend to implement the solutions presented, not partially ask it then change it. As is, your additional comment makes no sense and you need to rephrase the original question to clarify it, explaining how you need it to work. AND, show YOUR work. Jan 9, 2013 at 19:08
1
require 'open-uri'
source='http://www.example.com'
page = open(source).read
URI.extract(page,/.*\/aems\/file\/filegetrevision.do?fileEntityId=.*/)
2
  • +1 for your interest to my post.what package i need to install for that @alex? Jan 9, 2013 at 19:29
  • The feature is you don't need to install anything. OpenURI is a part of the standart ruby library.
    – alex
    Jan 9, 2013 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.