Powershel
Powershel

Reputation: 635

Get values from a website

My problem statement goes like this - I need to pull all hotel names and corresponding price from a web portal. If not via script, this is a tedious manual process for me.

For example on following URL I need name of all hotels with corresponding prices : http://hotel.makemytrip.com/makemytrip/site/hotels/search?session_cId=1403778791562&city=SLV&country=IN&checkin=06282014&checkout=06302014&area=&roomStayQualifier=1e0e&type=&sortName=&searchText=&isBaitNWait=null&fullSearch=false

Desired Output :

Hotel Name                     Price
Oberoi Wildflower Hall         16,500
Hotel Chaman Palace            1,879

I am doing it in Powershell language. Basically I need to understand how to get value of one placeholder (hotel name or price).So far I have tried this.

$surl="http://hotel.makemytrip.com/makemytrip/site/hotels/search?session_cId=1403778791562&city=SLV&country=IN&checkin=06282014&checkout=06302014&area=&roomStayQualifier=1e0e&type=&sortName=&searchText=&isBaitNWait=null&fullSearch=false"
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($surl)
$doc = $ie.Document
$element = $doc.getElementsByClassName("hotelImgLkflL")
$element > d:\element.txt

However, I am getting following error message.

You cannot call a method on a null-valued expression.

Update : Now I am trying to do it via $web.DownloadString and figured out that the source has following pattern for all Hotel Names :

id="200701171240402395" title="Oberoi Wildflower Hall" href="/makemytrip/site/hotels/detail?
id="201111211716292072" title="Hotel Chaman Palace" href="/makemytrip/site/hotels/detail?
id="200701121106345886" title="Hotel Baljees Regency" href="/makemytrip/site/hotels/detail?

How can I proceed now ? Thanks.

Appreciate any guidance.

Upvotes: 0

Views: 120

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Navigate() runs asynchronously, so you need to wait until the website is loaded completely before you can work on it:

...
$ie.navigate($surl)
while ( $ie.ReadyState -ne 4 ) { Start-Sleep -Milliseconds 100 }
$doc = $ie.Document
...

Upvotes: 1

Related Questions