Saturday, December 27, 2008

How to navigate through a web page and fetch data ?

Have you ever done clicking and moving mouse across 2-3 pages just to get one small piece of information. Now with on click you can browse through multiple pages and get the piece of info that you are looking for. Here, in this example I'll show you how to get the exchange rate of INR/USD and save that value into an txt file. This is for education purpose only. Stripping data from others website may be violation of law and in some cases frequent automated browsing migh bring down the site as well.




#include <IE.au3>
#include <String.au3>
#include <array.au3>
#Include <Date.au3>



Dim $iBasePrc, $chars, $QuoteFileName
$QuoteFileName = "Currency_" & @YEAR & ".txt"


$oIE = fnOpenURL("http://www.xe.com/ucc/", "3000")


; Initialize $sHTML value
$sHTML = _IEDocReadHTML ($oIE)


if StringLen($sHTML) > 1000 Then


$oForm = _IEFormGetObjByName ($oIE, "ucc")
$oSelect = _IEFormElementGetObjByName ($oForm, "From")
_IEFormElementOptionSelect ($oSelect, "United States Dollars - USD", 1, "byText")
$oSelect = _IEFormElementGetObjByName ($oForm, "To")
_IEFormElementOptionSelect ($oSelect, "India Rupees - INR", 1, "byText")
;Click on Submit image button
_IEFormImageClick ($oIE, "/gen/images/btn_go.gif", "src")
_IELoadWait ($oIE)
;now you are on rates page
$oTable = _IETableGetCollection ($oIE, 12)
$aTableData = _IETableWriteToArray ($oTable, True)
$myQuote = StringReplace($aTableData[2][2], "INR", "")
$myTime = _NowCalc();StringReplace($aTableData[1][0], "Live rates at ", "")
fnWriteToFile($QuoteFileName, $myTime & @TAB & $myQuote, "")


EndIf


_IEQuit ($oIE)



Func fnOpenURL($myURL, $AttachDelay)


ShellExecute ("C:\Program Files\Internet Explorer\iexplore.exe", "about:blank")
if $AttachDelay < 3000 Then $AttachDelay=3000;3 seconds
Sleep ($AttachDelay)
$oIE = _IEAttach ("about:blank", "url")
_IENavigate ($oIE, $myURL)
_IELoadWait ($oIE)
return($oIE)


EndFunc



Func fnWriteToFile($fileName, $Content, $TimeStamp)


$file = FileOpen($fileName, 1)
if $TimeStamp <> "" Then FileWriteLine($file, _NowCalc() & @TAB & $Content & @CRLF)
if $TimeStamp = "" Then FileWriteLine($file, $Content & @CRLF)


EndFunc


No comments:

Testing Google Search Results

Twin Star Adjustable Height Desk, White (ODP1055548D908) https://www.costcobusinessdelivery.com/Twin-Star-Adjustable-Height-Des...