Instead of going to the main page and trying to manipulate fields there, go to your actual browser and enter a sample destination and dates. You can also set the filters and sorting first too. Copy the URL and use VBA to change the values in the URL for your needs. Below is a piece of the URL. You can get the rest from your own testing.
Part of the URL is ?aDateRange%5Barr%5D=2021-01-19&aDateRange%5Bdep%5D=2021-01-21
so a VBA string value could be something like:
URL = "...?aDateRange%5Barr%5D=" & startDate & "&aDateRange%5Bdep%5D=" & endDate & ... 'Rest of URL
Once you access the page from VBA, you can parse the listings for the price(s), replace the start/end date, and grab the new page.
This is my first question so apologies if I haven’t grasped how I am meant to ask it
I have basic VBA and Python skills which I mostly use to manipulate Excel data in my job.
I want to build VBA code that will load trivago.co.uk Search a Hotel , from todays date for a certain number of days staying , and loop over the next say 100 days. Taking the prices and sorting these to find the day with the cheapest price.
I am really struggling as I have no experience with HTML at all but I’ve made a start.
I load the webpage and try to search the hotel by getelementbyiD(“”).value. Sometimes this work when I step through the code using F8 but when I run with F5 sometimes it doesn’t at all and just inputs a default hotel that I haven’t asked it to.
Ideally then I want it to click check in date and pick the “tonight” button but none of the buttons on this site have IDs that I can use .click on. After clicking check in date I want it to click the right arrow beside check out date as many times as would be input in my excel file. SO for example 2 night stay, it will click the right arrow beside check out once. The it would populate adults, children and rooms from cells in excel and click search.
I’m having real difficulty getting VBA to click buttons and cant understand why populating the search bar works sometimes but not others. Code and Webpage below
Any help at all would be great
Sub WebScrape()
Dim IEObject As InternetExplorer
Set IEObject = New InternetExplorer
IEObject.Visible = True
IEObject.navigate URL:="http://trivago.co.uk"
Do While IEObject.Busy = True Or IEObject.readyState <> READYSTATE_COMPLETE
Application.Wait Now + TimeValue("00:00:01")
Loop
Dim IEDocument As HTMLDocument
Set IEDocument = IEObject.document
IEObject.document.getElementById("querytext").Value = Range("Hotel").Value
Do While IEObject.Busy = True Or IEObject.readyState <> READYSTATE_COMPLETE
Application.Wait Now + TimeValue("00:00:01")
Loop
IEObject.document.getElementById("querytext").Value = Range("Hotel").Value
Dim oHTML_Element As IHTMLElement
For Each oHTML_Element In IEObject.document.getElementsByName("button")
If oHTML_Element.className = "icon-ic search-button__icon icon-center icon-contain" Then
oHTML_Element.Click
End If
Next
End Sub
That is a great idea, much simpler than what i was attempting. Thank you very much for that I will have a mess about with that now.
The input of the search bar is not in the URL though that might be a problem. Dates and rooms are in the URL but i cant see a way to add the hotel or area to the URL. Full URL for search:
@MatthewMcIlvanna I believe the cpt2 parameter (cpt2=402511%2F100) is the id of the hotel. If you’re only trying to track a few different hotels, you can just find and use the cpt2’s that you look up. If you want to search on the fly, you may be out of luck. You could prompt for the ID and use your browser to find the hotel. Not ideal, but it could work. There isn’t an easy way to search for places by name. It needs to know the exact location you want.
Also, does it need to be Trivago? There should be APIs you can leverage that provide a more straightforward way to get information, but not through Trivago.
Thanks icebird. No it doesn’t have to be Trivago by any means. I am just starting there, its just a sort of challenge I set myself having been on the site and noticed that while it compares other sights for the cheapest hotel room it doesn’t have a function that would allow you find the cheapest date at a specific hotel. More just my own project and i’m starting with Trivago as a test.