How to extract value from option tag instead of text

Information related to the use of iMacros for Web Scraping, Data Mining and creating Mashups.
Post Reply
ravi_npatty
Posts: 22
Joined: Sat May 07, 2011 6:12 am

How to extract value from option tag instead of text

Post by ravi_npatty » Fri May 20, 2011 5:08 pm

Dear iOpus,

Example html code:

<select name="fruits">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value=3>Mango</option>
</select>

I can extract the text of selected item in combobox (option tag)
TAG POS=1 TYPE=SELECT FORM=* ATTR=NAME:fruits EXTRACT=TXT
But I want to extract the value of the selected item. I could not find how to do this.

Please help me.
siniy
Posts: 118
Joined: Sat Nov 07, 2009 7:44 pm

Re: How to extract selected value instead of text from optio

Post by siniy » Fri May 20, 2011 9:30 pm

It works for your example. With real website you might need to tweak the regexp. You should learn regular expressions if you don't know them yet.

Code: Select all

'In case this is in loop
SET !EXTRACT NULL
TAG POS=1 TYPE=SELECT FORM=* ATTR=NAME:fruits EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}
SET !EXTRACT NULL
'Regexp searches for value by previously extracted text
SEARCH SOURCE=REGEXP:"<option value=\"([^\"])\">{{!VAR1}}" EXTRACT="$1"
PROMPT {{!EXTRACT}}
ravi_npatty
Posts: 22
Joined: Sat May 07, 2011 6:12 am

Re: How to extract selected value instead of text from optio

Post by ravi_npatty » Sat May 21, 2011 1:39 pm

Thanks siniy.

This did the trick. Thanks a lot. But direct option to extract the value like (TAG POS=1 TYPE=SELECT FORM=* ATTR=NAME:select_name EXTRACT=VALUE) would have been better.
Tom, Tech Support
Posts: 3440
Joined: Mon May 31, 2010 4:59 pm

Re: How to extract value from option tag instead of text

Post by Tom, Tech Support » Wed May 25, 2011 8:14 pm

Very clever siniy! Thanks for posting your solution!

I have moved this thread to our "How-To's and Examples for Web Scraping" forum and linked it from the Wiki.
Regards,

Tom, iMacros Support
Post Reply